Marcel Molina Jr.
7/19/2007 6:52:00 AM
On Thu, Jul 19, 2007 at 03:46:10PM +0900, Vin Raja wrote:
> I have following lines
>
> m=['a','b','c']
> puts m.each_with_index{|v,i| i}
>
> which output in:
>
> >ruby try.rb
> a
> b
> c
> >Exit code: 0
irb(main):003:0> m.each_with_index{|v,i| i}
=> ["a", "b", "c"]
irb(main):004:0> m.each_with_index{|v,i| p i}
0
1
2
=> ["a", "b", "c"]
The i variable in the block is indeed the index. But the result of the entire
each_with_index expression is not the result of each block invocation, but the collection
of all the array elements.
marcel
--
Marcel Molina Jr. <marcel@vernix.org>