From: dblack@... Date: 2002-11-27T13:49:10+09:00 Subject: Re: each_with_index & collect_with_index? Hi -- On Wed, 27 Nov 2002, Jason Persampieri wrote: > Why not have a global var (eg $# or something) that > keeps the current index whenever you're iterating > through an array/string/whatever? > > It would be consistent, extremely useful, less > confusing and completely straightforward. > > OK... let the criticism flow! (Aw, don't put it that way :-) Have a look at the "ToDo" file in the distribution -- you'll see: * discourage use of symbol variables (e.g. $/, etc.) in manual * discourage use of Perlish features by giving warnings. 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.... David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav