Is there a select and map for hashes instead of arrays?

J

Jeff

Hash#select and Hash#map return arrays. In some cases I want to get
hashes back instead.

I can take the returned arrays and transform them back into hashes,
but it would be nice to avoid the extra step.

Plus, it just surprised me that #select and #map aren't overridden for
Hash to return new Hashes instead of arrays.

Am I missing something?

Jeff
 
F

franco

# use two vars in an map/select block (or use the index if one var)
array = {:a => 1, :b =>2}.select {|k,v| k == :b}
# => [[:b, 2]]

# use inject to make a hash from a list, this is a great technique
hash = array.inject({}) {|h,e| h.merge({e[0] => e[1]}) }
# => {:b=>2}

-franco
 
J

Jeff

# use two vars in an map/select block (or use the index if one var)
array = {:a => 1, :b =>2}.select {|k,v| k == :b}
# => [[:b, 2]]

# use inject to make a hash from a list, this is a great technique
hash = array.inject({}) {|h,e| h.merge({e[0] => e[1]}) }
# => {:b=>2}

-franco

That was exactly my point - why doesn't Hash#select and Hash#map
return hashes instead of arrays?

Jeff
 
P

Peña, Botp

From: Jeff [mailto:[email protected]]=20
# I can take the returned arrays and transform them back into hashes,
# but it would be nice to avoid the extra step.
# Plus, it just surprised me that #select and #map aren't overridden for
# Hash to return new Hashes instead of arrays.
# Am I missing something?

none. just wait for 1.9 =
http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l81

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.

kind regards -botp
 
R

Rick DeNatale

With some changes to undo top-posting:
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"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top