From: Rick DeNatale Date: 2007-09-18T22:24:07+09:00 Subject: Re: Is there a select and map for hashes instead of arrays? With some changes to undo top-posting: On 9/18/07, James Herdman wrote: > On 9/17/07, Jeff wrote: > > 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: {:a => "fred", :b => "barney"}.map { |k,v| "#{k} is associated with #{v}" } # => ["a is associated with fred", "b is associated with barney"] -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/