Tom Clarke
Tom Clarke

Reputation: 23

How can I see the current state of a file buffer?

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:

  1. Is the file buffer being altered by gets?
  2. If the answer to question 1 is yes, then is there any way to see all the lines that still remain in the buffer?
  3. If the answer to question 2 is no, then I'm assuming that gets has 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?

Upvotes: 2

Views: 109

Answers (1)

Yoann Le Touche
Yoann Le Touche

Reputation: 1300

f.lineno will give you the current line number f.pos will give you the current offset in bytes

Upvotes: 1

Related Questions