I have a file called "file1.txt":
File.open('file1.txt', 'r') do |file|
while line = file.gets
puts "** " + line.chomp + " **" #--> why can't I use file.gets.chomp?
line = file.gets
file.gets = line
I'm not really into Ruby, but I think that is because if you use
while line = file.gets, the
file.gets return a line and read (and copy to buffer) the next one. In the final iteration, where the while is in the last line, the
while line = file.gets will return the last line. But in the while, you call again
file.gets, so as there are no more lines in file, it returns an error.