Merge an array of hashes

C

cleaner416

For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Any suggestions?
 
J

James Edward Gray II

For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Any suggestions?
Sure:
a = [{:some_key => :some_value}, {:another_key => :another_value}] => [{:some_key=>:some_value}, {:another_key=>:another_value}]
a.inject { |all, h| all.merge(h) }
=> {:some_key=>:some_value, :another_key=>:another_value}

James Edward Gray II
 
B

Bob Showalter

For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Hash[*a.collect {|h| h.to_a}.flatten]
 
L

Luis Parravicini

For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Any suggestions?

hash = Hash.new
a.each { |h| hash.merge! h }
 
J

Joel VanderWerf

Luis said:
For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so

a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]

And I would like to flatten it to single hash in one line so I get

{ :some_key => :some_value, :another_key => :another_value }

Any suggestions?

hash = Hash.new
a.each { |h| hash.merge! h }

Or the equivalent using #inject, if you like:

a = [ { :some_key => :some_value }, { :another_key => :another_value } ]

merged = a.inject({}) {|acc, h| acc.merge! h}

p merged # ==> {:some_key=>:some_value, :another_key=>:another_value}
 
P

Phrogz

Luis said:
For some reason I can't get my head wrapped around this trivial
exercise. I have an array of hashes like so
a = [ { :some_key => :some_value }, { :another_key
=> :another_value } ]
And I would like to flatten it to single hash in one line so I get
{ :some_key => :some_value, :another_key => :another_value }
Any suggestions?
hash = Hash.new
a.each { |h| hash.merge! h }

Or the equivalent using #inject, if you like:

a = [ { :some_key => :some_value }, { :another_key => :another_value } ]

merged = a.inject({}) {|acc, h| acc.merge! h}

p merged # ==> {:some_key=>:some_value, :another_key=>:another_value}

....which brings us back (almost) to JEGII's original response :)

Sure:
a = [{:some_key => :some_value}, {:another_key => :another_value}] => [{:some_key=>:some_value}, {:another_key=>:another_value}]
a.inject { |all, h| all.merge(h) }
=> {:some_key=>:some_value, :another_key=>:another_value}
 
J

Joel VanderWerf

Phrogz said:
...
...which brings us back (almost) to JEGII's original response :)

Sorry, my bad :/

But note that #merge! is probably slightly faster in this case than
#merge, since it reuses the same hash. This is safe in this case because
the accumulated hash is not referenced elsewhere during the iteration.
 
R

Rubén Medellín

Hash[*a.collect {|h| h.to_a}.flatten]

I wouldn't recommend this.

a = [{:an_array => [1,2,3,4]}, {:another_array => [5,6,7,8]}]
Hash[*a.collect {|h| h.to_a}.flatten]

=> { :an_array => 1, 2 => 3, 4 => :another_array, 5 => 6, 7 => 8 }

There is no problem of course if there are no lists in the hash.
 
Y

Yossef Mendelssohn

Hash[*a.collect {|h| h.to_a}.flatten]

I wouldn't recommend this.

a =3D [{:an_array =3D> [1,2,3,4]}, {:another_array =3D> [5,6,7,8]}]
Hash[*a.collect {|h| h.to_a}.flatten]

=3D> { :an_array =3D> 1, 2 =3D> 3, 4 =3D> :another_array, 5 =3D> 6, 7 =3D=
8 }

There is no problem of course if there are no lists in the hash.

2 is 3, 5 is 6, and 7 is 8?

My entire worldview has gone topsy-turvy.
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top