# Re: getting the next line with the iterator

**URL:** <https://www.ruby-forum.com/t/re-getting-the-next-line-with-the-iterator/59043>\
**Category:** Ruby\
**Created:** [April 28, 2006, 3:51am UTC](https://www.ruby-forum.com/t/re-getting-the-next-line-with-the-iterator/59043 "2006-04-28T03:51:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [April 28, 2006, 3:51am UTC](https://www.ruby-forum.com/t/re-getting-the-next-line-with-the-iterator/59043/1 "2006-04-28T03:51:07Z")

</div>

> 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
