Hi,
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:
list = []
File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|
if line =~ /regExp/
list << line.nextIterator
list << line.nextIterator
list << line.nextIterator
end
}
end
puts list
This code is not working, I know. The method “nextIterator” doesn’t
exist. This is the way I see it, but I don’t know how to do it. I think
that you can invode the method “next” on iterators in Python. I don’t
know how to do that without invoking the command ‘next’ with a flag and
a counter, a little bit like this code (which is not very rubyist):
list = []
flagIn = false
counter = 3
File.open(‘toto.txt’, ‘rb’) do |myFile|
myFile.each {|line|
if line =~ /regExp/i
flagIn = true
next
end
if flagIn
list << line
counter -= 1
flagIn = false if counter == 0
end
}
end
puts list
Any suggestions?
Thanks