From: Martin DeMello Date: 2002-11-27T18:53:15+09:00 Subject: Re: each_with_index & collect_with_index? Alan Chen wrote: > Well, how about something like Regexp.last_match, where you can > make a call to retrieve the iteration data. > > ary.map { |e,f,g| > ei = Enumerable.iteration_info > #... > } The problem is that Enumerable is built around a class-supplied 'each', and each itself runs in a very black-box manner, communicating only through yield. I don't see how Enumerable would get the info, short of enforcing an each_with_index requirement, and rewriting 'each' as a wrapper around 'each_with_index'. Perhaps this might work (untested): module Enumerable alias _each each def each Enumerable.iteration = 0 _each {|*i| Enumerable.iteration += 1 yield *i } end end but note that the number of times you've iterated is not necessarily the same as what you'd want _with_index to return. martin