2007/7/19, Vin Raja <vineetraja / gmail.com>:
> Hi All,
>
> 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
>
>
>
> I was expecting to see indexes, rather than values.
> Am I overlooking some trivial nuance or what.
> Basically i wanted to print index values while iterating an array at
> some other project.
> and "each_with_index" seemed very obvious for the task but for the
> unexpected result.

It is. But #each_with_index *returns* the Enumerable.  You need to put
the put into the block:

irb(main):001:0> ['a','b','c'].each_with_index {|a,i| puts i}
0
1
2
=> ["a", "b", "c"]
irb(main):002:0>

Kind regards

robert