From: Robert Klemme Date: 2009-09-13T01:10:07+09:00 Subject: Re: Backtracking through a file On 12.09.2009 17:57, M. Q. wrote: > IO.gets steps through a file one line at a time. However, I cannot > figure out how to (once i find something i need in the file) go > backwards line by line through that file until i find something else. I > thought possibly setting IO.lineno to the line before the current line > that would work, but I think I just misunderstood what lineno was for. > Anyone know a good way to do this? One option is to create an Array of positions obtained via IO#tell while you go along. Then you can move backwards to every line start. Something like File.open "foo" do |io| idx = [io.tell] while l = io.gets p l idx << io.tell end end Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/