2010/5/31 botp <botpena / gmail.com>:
> On Mon, May 31, 2010 at 9:04 AM, Bug Free <amberarrow / yahoo.com> wrote:
>> The following line:
>>
>> =A0 =A0[5, 7].each_with_index.each_cons(2) {|v| p v }
>>
>> prints [5, 7] but I'm expecting [[5, 0], [7, 1]].
>
> you'll have specify the index, eg,
>
>>> =A0[5, 7].map.with_index{|v,i| [v,i]}
> =3D> [[5, 0], [7, 1]]

That's not the proper replacement with map.  Rather you'd do:

irb(main):009:0> [5, 7].each_with_index.map {|v| v }
=3D> [5, 7]
irb(main):010:0> [5, 7].each_with_index.map {|*v| v }
=3D> [[5, 0], [7, 1]]

Interestingly the splat operator does not include the index with #each_cons=
:

irb(main):011:0> [5, 7].each_with_index.each_cons(2) {|v| p v }
[5, 7]
=3D> nil
irb(main):012:0> [5, 7].each_with_index.each_cons(2) {|*v| p v }
[[5, 7]]
=3D> nil

This is on ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-cygwin].

I'd say it's a bug which seems to be reflected by the fact that it
works as expected in 1.9.3 according to Caleb.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/