Alan Chen <alan / digikata.com> 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