Working through this problem and I have typed out the code EXACTLY like the problem states - even tried to copy and paste to see if it was something I was doing wrong but its not.
The code that I have is at the bottom of this post. I am sending the argument 'test.txt' which contains:
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here
however, when i run the code, during the print_all(current_file) it ONLY prints 'lots and lots of fun to have here.' - which is the last line of the file.
And where it is supposed to print out each individual line, it prints:
1 ["This is stuff I typed into a file. \rIt is really cool stuff. \rLots and lots of fun to have in here.\r\r"]
2 []
3 []'
essentially capturing all lines as 1 line, and printing nothing where its supposed to print line 2 and 3.
Any ideas?
input_file = ARGV[0]
def print_all(f)
puts f.read()
end
def rewind(f)
f.seek(0, IO::SEEK_SET)
end
def print_a_line(line_count, f)
puts "#{line_count} #{f.readlines()}"
end
current_file = File.open(input_file)
puts "First let's print the whole file:"
puts # a blank line
print_all(current_file)
puts "Now let's rewind, kind of like a tape."
rewind(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)