[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Jeff

9/17/2007 10:05:00 PM

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


5 Answers

flazzarino

9/18/2007 1:27:00 AM

0

# 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

On Sep 17, 6:05 pm, Jeff <cohen.j...@gmail.com> wrote:
> 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


Jeff

9/18/2007 3:09:00 AM

0

On Sep 17, 8:35 pm, franco <flazzar...@gmail.com> wrote:
> # 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


Peña, Botp

9/18/2007 4:45:00 AM

0

From: Jeff [mailto:cohen.jeff@gmail.com]
# 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+Ru...

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

Rick DeNatale

9/18/2007 1:24:00 PM

0

With some changes to undo top-posting:
On 9/18/07, James Herdman <james.herdman@gmail.com> wrote:
> On 9/17/07, Jeff <cohen.jeff@gmail.com> 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.denh...

Jeff

9/18/2007 10:17:00 PM

0

On Sep 17, 11:45 pm, Pe?a, Botp <b...@delmonte-phil.com> wrote:
> From: Jeff [mailto:cohen.j...@gmail.com]
> just wait for 1.9http://eigenclass.org/hiki.rb?Changes+in+Ru...

Thanks for the info.

Jeff