On Wed, Jun 4, 2008 at 1:18 PM, Luke Grimstrup <luke.g / inner-fire.net> wrote:
> Close, but not quite.

You already got other solutions, but here is another one:

irb(main):001:0> a = [{:id => 1, :name => "a"}, {:id => 2, :name => "b"}]
irb(main):002:0> b = [{:other => "other_b_data", :name => "b"},
{:other => "other_a_data", :name => "a"}]
irb(main):003:0> h = Hash.new {|h,k| h[k] = {}}
irb(main):013:0> (a + b).each {|data| h[data[:name]].merge!(data)}
irb(main):015:0> h.values
=> [{:other=>"other_a_data", :name=>"a", :id=>1},
{:other=>"other_b_data", :name=>"b", :id=>2}]

I haven't measured the time, but this might have better performance
than finding in the second array by
name every time, since here we are accessing a hash by that key.

Hope this helps,

Jesus.