From: Robert Klemme Date: 2010-06-01T17:14:35+09:00 Subject: Re: enumerator problem in 1.9.1 2010/6/1 botp : > On Mon, May 31, 2010 at 9:53 PM, Robert Klemme > wrote: >> Because it is closer on what OP initially did: > > LOL. ok i get it :) > i was just pointing out how to retrieve the index (and the relevance > of it). i wouldn't want to "give" him *the* answer because i think > rubyist _are_ programmers... > > kind regards -botp > >> irb(main):001:0> [5, 7].each_with_index.each {|*a| p a} >> [5, 0] >> [7, 1] >> => [5, 7] > > or simply, > > => [5, 7].each_with_index{|*a| p a} > [5, 0] > [7, 1] > => [5, 7] No, this does not create an intermediate Enumerator. For trying to hunt down a potential bug this does not help because it leaves the Enumerator creation totally out of the loop. >> This means that in your code the index cannot get lost along the way >> simply because it is added in the last step and not in the initial >> step. > > shouldn't the programmer be the judge for that? > > eg, i can easily scale if i need the square and the index... > >> [5, 7].map{|a| a*a }.each_with_index{|*a| p a} > [25, 0] > [49, 1] I think we are talking past each other. IMHO this thread is about a potential bug and you are talking about a solution with regard to getting an index while iterating. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/