From: "林彰史" Date: 2012-12-12T20:11:43+09:00 Subject: Enumerable#zip with File IO --e89a8f13eaf8e103f404d0a5dce9 Content-Type: text/plain; charset=ISO-8859-1 Hello, I found strange behavior of Enumerable#zip, which I could not understand why. Appreciate if anyone can explain. I wrote a short script below to see how Enumerable#zip method works with File IO. file = open( ARGV[0], "r" ) [1].zip( file.each ) do |n,l| puts "#{n}: #{l}" end [2].zip( file.each ) do |n,l| puts "#{n}: #{l}" end Then, I provided it with a text file: 111 222 333 I expected that its output would be: 1: 111 2: 222 but actually got: 1: 111 2: 333 It looked like the second line of input was accidentally discarded. If I replaced the arrays with ranges like: (1..1).zip( file.each ) do |n,l| puts "#{n}: #{l}" end (2..2).zip( file.each ) do |n,l| puts "#{n}: #{l}" end then I got the expected result. Ruby version is ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12.2.1]. Thanks in advance, Hayashi --e89a8f13eaf8e103f404d0a5dce9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hello,

I=A0found strange behavior of Enumerab= le#zip, which I could not=A0understand why.
Appreciate if anyone = can explain.

I wrote a short script below to see h= ow Enumerable#zip method works with File IO.

=A0 file =3D open( ARGV[0], "r" )=A0
=A0 [1].zip( file.each ) do |n,l| puts "#{n}: #{l}" end=A0
=A0 [2].zip( file.each ) do |n,l| puts "#{n}: #{l}" end=A0<= /div>

Then, I provided it with a text file:

111=A0
222=A0
333=A0

I expe= cted that its output would be:

1: 111=A0
2: 222=A0

but actually got:

1: 111=A0
2: 333

It looked like the second line of input = was accidentally discarded.

If I replac= ed the arrays with ranges like:

=A0=A0(1..1).zip( file.each ) do |n,l| puts "#{n}:= #{l}" end=A0
=A0 (2..2).zip( file.each ) do |n,l| puts &quo= t;#{n}: #{l}" end=A0

then I got the expected = result.

Ruby version is=A0ruby 1.9.3p327 (2012-11-10 revision 3= 7606) [x86_64-darwin12.2.1].

Thanks in advance,
Hayashi

--e89a8f13eaf8e103f404d0a5dce9--