From: Martin DeMello Date: 2002-11-27T22:13:32+09:00 Subject: Re: each_with_index & collect_with_index? ts wrote: >>>>>> "M" == Martin DeMello writes: > > M> def each > M> Enumerable.iteration = 0 > > and if someone write : > > [[1, 2], [3, 4]].each {|i| i.each {|j| ... }} Doh! What *is* the correct thing to do in such a case, without having to introduce special syntax? Best I can think of is to have Enumerable.iteration return a stack, which would be inconvenient to use. Or maybe maintain a stack internally, but have a separate accessor for the top of the stack (i.e. the innermost block count) That might work out, actually module Enumerable def count (@@iteration ||= []).last end def counts @@iteration or [] end alias _each each def each (@@iteration ||= []).push(0) _each {|*i| @@iteration.last += 1 yield *i } @@iteration.pop end end Another random thought: can you get at the names of the block local variables? Might be possible to hack something up along the lines of a hash of symbols, if so. martin