From: dblack@... Date: 2003-04-25T07:40:47+09:00 Subject: Re: Hashes and Enumerable#each_with_index Hi -- On Fri, 25 Apr 2003, Ryan Pavlik wrote: > OK, looking at the archives I know this was discussed a few years ago, > but there seems to be no real conclusion or improvement since then. > > The problem: Hash#each_with_index returns an index that is not usable > to address the hash. That is, given the following: > > h = Hash.new > h.each_with_index { > | o, i | > ... > } > > i is not defined such that h[i] == o, which is both misleading and > seemingly useless. (This behavior, as per the previous discussion, can > be achieved with h.to_a.each_with_index.) > > Would it not be more useful to adopt the definiton of > Enumerable#each_with_index where the index given is such that e[i] == o > for any given Enumerable? One problem I see is: how could you implement this, in Enumerable, in terms of #each? The current #each_with_index is essentially: i = 0 each {|e| yield(e,i); i += 1} If the new method (which I think should have a new name :-) did the above for arrays, but did this for hashes: each {|k,v| yield(v,k)} you'd be thrown back on defining something differently for different classes that mixed in Enumerable. David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav