From: "David A. Black" Date: 2008-06-04T22:01:09+09:00 Subject: Re: Mering an array of hashes Hi -- On Wed, 4 Jun 2008, Luke Grimstrup wrote: > Actually, complete code: > a1 = [{:name=>"File1.rar", > :id=>"a3d80425a51a3a4cb2d7a3201439ef6cc3af5874"}, {:name=>"File2.avi", > :id=>"58a658047ccb7333eb812da02dca9d84c1db2b41"}, {:name=>"File3.avi", > :id=>"944f6a7c50a4a5cff0c28cb0f9158b16580b0310"}] > > a2 = [{:name=>"File2.avi", :other_attributes =>"(350 MiB) - 2.85% > downloading at 0.00 B/s (UL at 0.00 B/s), stalled [error > (tracker-warning) - Tracker hasn't responded yet. Retrying...]"}, > {:name=>"File1.rar", :other_attributes=>" (26.6 MiB) - 100% seeding at > 0.00 B/s [0.23]"}, {:name=>"File3.avi", :other_attributes=>"(35.5 MiB) - > 0.86% downloading at 0.00 B/s (UL at 0.00 B/s), stalled"}] > > a2.each{|d| d.merge!(a1.select{|e| e[:name] == d[:name]}.first)} > > Luke Grimstrup wrote: >> Ah, that does work too! >> >> A friend of mine told me what I "should" be doing to get it working, but >> left the exercise of the coding it up to me. >> >> Here's the magic: >> hash_b.each{|d| d.merge!(hash_a.select{|e| e[:name] == d[:name]}.first)} You can use #find rather than #select plus #first. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!