Hashes and Enumerable#each_with_index

Hi –

···

On Sun, 27 Apr 2003, Sam Roberts wrote:

Quoteing rpav@nwlink.com, on Fri, Apr 25, 2003 at 07:26:01AM +0900:

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.)

This function already exists, for a Hash its called Hash#each.

Not exactly: Hash#each returns [k,v] pairs, not [v,k] pairs. The
[element,index] style of Array#each_with_index is more similar to
the latter than the former (i.e., array[index] == element, and
hash[k] = v).

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav