From: Austin Ziegler Date: 2002-11-27T23:14:10+09:00 Subject: Re: each_with_index & collect_with_index? On Wed, 27 Nov 2002 14:51:41 +0900, Alan Chen wrote: > On Wed, Nov 27, 2002 at 01:49:10PM +0900, > dblack@candle.superlink.net wrote: >> So a new global of that kind probably isn't on the horizon. I was >> playing around with extending objects during iteration, so that >> you could do: >> >> ary.map {|e| puts "We're at iteration #{e.iteration}"} >> >> or something -- but I haven't come up with a way to do it that >> fits more than a subset of cases. For one thing, you can't extend >> Fixnums, so anything based on that method won't work if your >> array contains those. Also, the syntax of block parameters makes >> it hard: if you have >> >> ary.map {|e,f,g| ... } >> >> I'm not sure there's a way to determine which object got the >> #iteration method added to it.... > 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 > #... > } I like that, sort of. It still has the problem that a global does; why not make it an importable method for Enumerable? ary.map { |e, f, g| ei = ary.iteration_info #... } This doesn't work in cases where the same Enumerable could be iterated more than once (either by Threading or by nesting), but I think that those are pathological cases anyway. -austin -- Austin Ziegler, austin@halostatue.ca on 2002.11.27 at 09.11.52