Forum: Ruby Re: getting the next line with the iterator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel S. (Guest)
on 2006-04-28 05:51
(Received via mailing list)
> 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:

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
This topic is locked and can not be replied to.