Try:
a.each_with_index.map{|pr,last| p "pr: #{pr} last: #{last}"}
map
is automatically deconstructing the values passed to it. The next question is why is it doing this deconstruction and select
isn't?
If you look at the source given on the Rdoc page for Array they're virtually identical, select
only differs in that it does a test on the value yielded. There must be something happening elsewhere.
If we look at the Rubinius source (mainly because I'm better with Ruby than C;) for map
(aliased from collect
) it shows us:
each do |*o|
so it's splatting the arguments on the way through, whereas select (aliased from find_all
) does not:
each do
again, the design decision as to why is beyond me. You'll have to find out who wrote it, maybe ask Matz :)
I should add, looking at the Rubinius source again, map
actual splats on each
and on yield
, I don't understand why you'd do both when only the yield splat is needed:
each do |*o|
ary << yield(*o)
end
whereas select
doesn't.
each do
o = Rubinius.single_block_arg
ary << o if yield(o)
end