From: benjohn@... Date: 2008-06-04T20:56:24+09:00 Subject: Re: Mering an array of hashes > Sorry B, late at night here :p > > array #1: [{:name=>"File1.rar", > :id=>"a3d80425a51a3a4cb2d7a3201439ef6cc3af5874"}, {:name=>"File2.avi", > :id=>"58a658047ccb7333eb812da02dca9d84c1db2b41"}, {:name=>"File3.avi", > :id=>"944f6a7c50a4a5cff0c28cb0f9158b16580b0310"}] > > array #2 [{:name=>"File2.avi", :other_attributes =>"stuff"}, > {:name=>"File1.rar", :other_attributes=>"stuff"}, {:name=>"File3.avi", > :other_attributes=>"stuff"}] > > And I would like the resulting array to look like: > array #1: [{:other_attributes => "stuff", :name=>"File1.rar", > :id=>"a3d80425a51a3a4cb2d7a3201439ef6cc3af5874"}, {:name=>"File2.avi", > :id=>"58a658047ccb7333eb812da02dca9d84c1db2b41", :other_attributes => > "stuff"}, {:name=>"File3.avi", > :id=>"944f6a7c50a4a5cff0c28cb0f9158b16580b0310", :other_attributes => > "stuff"}] > > But I just have no idea how to do it. I'm sure there's an easy ruby way > to do it somehow.. Ah, okay, that's clear, but pretty weird data :-) id_rows = a.map {|r| r.values_at(:name, :id)} other_att_rows = b.map {|r| r.values_at(:name, :other_attributes)} name_to_attr_map = Hash[*other_att_rows.flatten] output = id_rows.map {|r| Hash[:name, r[0], :id, r[1], :other_attributes, name_to_attr_map[r[0]]]} Will work, and can be readily improved (exercise for reader ;-) - it'll be niceer without turning the data in to an array of rows as I do in first two lines).