Yes I agree that it makes no sense, but I wanted to see how zip method works
with File IO with a simple experimental code.
My point is why the second line of input is discarded.
That might be an artifact of unhealthy using of the Enumerator because
you do not really iterate through the whole file but you use it twice
on the same file. Chances are that the Enumerator fetches the next
item before it is actually uses and #zip just breaks out of the loop
prematurely. In your example you’d rather want
[1, 2].zip( file.each ) do |n,l| puts “#{n}: #{l}” end
Yes I agree that it makes no sense, but I wanted to see how zip method
works with File IO with a simple experimental code.
My point is why the second line of input is discarded.
I have tried the same script with JRuby 1.7.0 also, and I got:
1: 111
2:
Not only the second line but also the third line disappears.
Replacing arrays with ranges did not change the output.
I checked StringIO also and got the same.
So, my conclusion is that Enumerable#zip can be applied to IO
Enumerable object only once, as Robert pointed out. If I accidentally
do it twice, the result of the second application is implementation
dependent.
This is what I did not know.