Return to Snippet

Revision: 19261
at October 20, 2009 03:49 by chrisaiv


Initial Code
#The Merge method is for Hashes only.  
h1 = { "a" => 111, "b" => 222 }
h2 = { "b" => 333, "c" => 444 }

#Conflict resolution
puts h1.merge(h2) { |key, old_value, new_value| old_value }
puts h1.merge(h2) { |key, old_value, new_value| new_value }


#Longhand
puts h1.merge(h2) do |key, old_value, new_value|
  if old_value < new
    old_value
  else
    new_value
  end
end

#Shorthand
puts h1.merge(h2) { |k, o, n| o < n ? o : n }

Initial URL


Initial Description


Initial Title
Ruby: How to use the Merge method within Ruby Blocks

Initial Tags
ruby

Initial Language
Ruby