Given the following code:
File.open('file1.txt', 'r') do |file|
while line = file.gets
puts "** " + line.chomp.reverse + " **"
end
end
I am confused to what is the question being asked? This is a simple piece of code I got off my tutorial, that reads a file's lines and puts it out. I do understand most of it, I believe you are assigning a variable line to the return value of file.gets, and it retrieves the value of those lines, and puts it out.
Where I am having trouble is the initial loop statement: while line = file gets
My question is that what kind of question are you asking and how does it break out of the loop?
i.e.:x=3 x ==3--> You are asking is X equal to 3, if true will return true, if false will return false.
Also, are you simultaneously assigning the return value of file.gets to the variable line, in addition to putting it in the while statement?
IO.foreach("file1.txt") { |line| puts "** " + line.chomp.reverse + " **" }. See IO::foreach. (This is often writtenFile.foreach(..., which works becauseFileis a subclass ofIO).