I want to gain some insight into how Ruby manages file buffering. I looked elsewhere for the answers, but I guess I’m not asking the right questions.
In an IRB session I opened a file for reading:
f = File.open('somefile.txt', 'r')
Using this command:
puts f.gets
prints out the first line of somefile.txt. If I repeat the puts f.gets command, I get the second line, and so on.
My questions are:
- Is the file buffer being altered by
gets? - If the answer to question 1 is yes, then is there any way to see all the lines that still remain in the buffer?
- If the answer to question 2 is no, then I’m assuming that
getshas some record of the last line of the file that it read. Is there any way to find out the value of this line index?
f.linenowill give you the current line numberf.poswill give you the current offset in bytes