From: Stefano Crocco Date: 2007-07-19T15:53:29+09:00 Subject: Re: Array : each_with_index Alle gioved狸 19 luglio 2007, Vin Raja ha scritto: > 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. > > Raja You're telling ruby to print the return value of each with index, which, according to the ri documentation, is the array itself. If you want to print the different inidces, you need to put the call to puts inside the block: m.each_with_index{|v,i| puts( i )} Putting i as the last statement of the block simply makes that the return value of the block, but this is ignored by each_with_index, so it's useless. I hope this helps Stefano