From: James Edward Gray II Date: 2007-09-13T03:59:42+09:00 Subject: Re: Merge an array of hashes On Sep 12, 2007, at 1:55 PM, cleaner416 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? 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