flazzarino
9/18/2007 1:27:00 AM
# 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