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?

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

···

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

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

···

On 9/12/07, cleaner416 <cleaner416@gmail.com> 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 = Hash.new
a.each { |h| hash.merge! h }

···

On 9/12/07, cleaner416 <cleaner416@gmail.com> 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 }

Any suggestions?

--
Luis Parravicini
http://ktulu.com.ar/blog/

Thanks!!!!!!!!

Luis Parravicini wrote:

···

On 9/12/07, cleaner416 <cleaner416@gmail.com> 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 }

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}

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  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.

Luis Parravicini 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 }

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

···

On Sep 12, 1:20 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:

> On 9/12/07, cleaner416 <cleaner...@gmail.com> wrote:

On Sep 12, 12:59 pm, James Edward Gray II <ja...@grayproductions.net> wrote:

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}

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

My entire worldview has gone topsy-turvy.

···

On Sep 13, 1:10 am, Rubén Medellín <CHub...@gmail.com> 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.

--
-yossef

Phrogz 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.

···

On Sep 12, 1:20 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407