From: dblack@... Date: 2002-11-26T12:45:40+09:00 Subject: Re: each_with_index & collect_with_index? Hi -- On Tue, 26 Nov 2002, Tim Bates wrote: > Array.each (and others) have an alternative .each_index which passes the index > to the element instead of the element itself to the block. Is there an easy > way to pass both the index and the element, eg something like > Array.each_with_index{ |i, el| ... } Yes, there is such a method. Your notation is a bit off, because it looks like a class method of Array instead of an instance method of arrays (actually of enumerables), and the element comes first: ar.each_with_index {|el,i| ... } but anyway, it exists. > Also, an equivalent for these in 'collect' could be useful, eg > Array.collect_index{ |i| ... } Array.collect_with_index{ |i, el| > ... } How about it? The first one can be done with: (0...array.size).map {|i| ... } though it's not symmetrical as to the wording. As for the second.... I recently appointed myself President of Citizens for MWI [map_with_indices], Inc. (See ). You may join the organization, free of charge :-) Anyway, there's been a lot of talk of this at various points; if you search for map_with_index and map_with_indices on the ruby-talk archive, you'll see most of it. Matz has recently indicated that he doesn't think there's a demand or need for it. I'm still trying to keep the dream alive :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav