Merge an array of hashes

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?

On Sep 12, 2007, at 1:55 PM, cleaner416 wrote:

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 G. II

On 9/12/07, cleaner416 [email protected] wrote:

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]

Thanks!!!

On 9/12/07, cleaner416 [email protected] wrote:

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

On Sep 12, 1:20 pm, Joel VanderWerf [email protected] wrote:

{ :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 :slight_smile:

On Sep 12, 12:59 pm, James Edward G. II [email protected]

Luis P. wrote:

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}

Phrogz wrote:

On Sep 12, 1:20 pm, Joel VanderWerf [email protected] wrote:

Or the equivalent using #inject, if you like:

…which brings us back (almost) to JEGII’s original response :slight_smile:

Sorry, my bad :confused:

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.

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.

On Sep 13, 1:10 am, Rubén Medellín [email protected] wrote:

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.

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

My entire worldview has gone topsy-turvy.