Hi Jeff.
My guess is because you need to enumerate over a Hash. Recall that
entries
in a Hash are indexed by some Hashing algorithm, so it’s kind of had to
enumerate over it’s entries.
It’s probably cheaper to toss all the entries in a Hash into an array
and
iterate over it’s index than to try and divine some means of iterating
over
the non-sequential Hash table containing index values.
i think this is an faq. in this regard, i’d like to request that the
ruby faq be on the frontpage of ruby-lang.org and ruby-doc.org and that
it be mirrored.
why doesn’t Hash#select and Hash#map
return hashes instead of arrays?
My guess is because you need to enumerate over a Hash. Recall that entries
in a Hash are indexed by some Hashing algorithm, so it’s kind of had to
enumerate over it’s entries.
There’s no problem enumerating over a Hash. Hash#each is implemented
after all. Enumerable only requires that each yield all of the
elements in SOME order, the fact that Hashes don’t have a natural
ordering doesn’t hamper this. Hash#each yields key,value pairs.
Ruby 1.9 is on track to change Hash#select to return a Hash.
Hash#map is problematic though, map returns a new enumeration which
contains the elements resulting in applying the block to each element
yielded by each. In general this could be anything for example: