I find this the most readable way to do it, though using generator is
probably slower than just keeping track of the last 3 lines as you go
forward:
require 'generator'
list =
File.open('toto.txt', 'rb') do |myFile|
g = Generator.new(myFile)
while g.next?
if g.next =~ /regExp/
3.times { list << g.next }
end
end
end
puts list
···
-----Original Message-----
From: Eric Boucher [mailto:devlists-ruby-talk@devlists.com]
Sent: Friday, 28 April 2006 8:21 AM
To: ruby-talk ML
Subject: getting the next line with the iteratorHi,
I would like to know if there is a ruby way to achieve what I
want to
do. I want to be able to access the next iterator while
reading a file.
Basically, I want to be able to collect the next 3 lines when I
encounter a certain line: