tags:

views:

172

answers:

3

Is there a simple way of remapping a hash in ruby the following way:

from:

{:name => "foo", :value => "bar"}

to:

{"foo" => "bar"}

Preferably in a way that makes it simple to do this operation while iterating over an array of this type of hashes:

from:

[{:name => "foo", :value => "bar"}, {:name => "foo2", :value => "bar2"}]

to:

{"foo" => "bar", "foo2" => "bar2"}

Thanks.

+8  A: 
arr = [ {:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

result = {}
arr.each{|x| result[x[:name]] = x[:value]}

# result is now {"foo2"=>"bar2", "foo"=>"bar"}
Gishu
Yes, this is almost what I did also, but I used x.values[0] and x.values[1] instead of x[:name] and x[:value], in case I don't know the keys in advance. I was fishing for an even simpler way to do this, but there might not be one...
morbaq
Hash values are out of order so you can't do x.values[0] and x.values[1]
Vanson Samuel
+1 to Vanson.. you can see that the result contents are already out of order since its a Hash.
Gishu
Yes, I didn't think of that. +1 for pointing that out.
morbaq
+3  A: 

A modified version of Vanson Samuel's code does the intended. It's a one-liner, but quite a long one.

arr = [{:name=>"foo", :value=>"bar"}, {:name=>"foo2", :value=>"bar2"}]

arr.inject({}){|r,item| r.merge({item['name'] => item['value']})}

# result {"foo" => "bar", "foo2" => "bar2"}

I wouldn't say that it's prettier than Gishu's version, though.

morbaq
+1  A: 

As a general rule of thumb, if you have a hash of the form {:name => "foo", :value => "bar"}, you're usually better off with using a tuple of ["foo", "bar"].

arr = [["foo", "bar"], ["foo2", "bar2"]]
arr.inject({}) { |accu, (key, value)| accu[key] = value; accu }
Bob Aman