168

Is there an easy or elegant way to merge two hashes without overwriting duplicate keys?

That is, if the key is present in the original hash I don't want to change its value.

2
  • Do you really mean arrays (eg: ['a', 'b', 'c']) or hashes (eg: {'a' => 1, 'b' => 2, 'c' => 3})? Dec 30, 2009 at 15:43
  • Sorry, I was talking about hashes :) Dec 30, 2009 at 16:19

5 Answers 5

277

If you have two hashes, options and defaults, and you want to merge defaults into options without overwriting existing keys, what you really want to do is the reverse: merge options into defaults:

options = defaults.merge(options)

Or, if you're using Rails you can do:

options.reverse_merge!(defaults)
5
  • Totally agree, thanks a lot for reverse_merge! method did not know it :) Dec 30, 2009 at 15:58
  • WHy is the parentheses needed here? You cant just do default.merge options it appears.
    – Donato
    Apr 24, 2015 at 20:49
  • 2
    They are deprecating reverse_merge! due to security issues in rails 5.1 Jun 12, 2018 at 13:00
  • 1
    @Mirv-Matt - I don't see a depreciation notice. apidock.com/rails/v6.0.0/Hash/reverse_merge%21
    – Kshitij
    Jan 14, 2020 at 14:36
  • 1
    @Donato a common convention in Ruby is to use parens when returning a value, even if they are not strictly needed (sorry to answer 7 years later)
    – mtjhax
    Apr 18, 2022 at 19:32
23

There is a way in standard Ruby library to merge Hashes without overwriting existing values or reassigning the hash.

important_hash.merge!(defaults) { |key, important, default| important }
3

If your problems is that the original hash and the second one both may have duplicate keys and you don't want to overwrite in either direction, you might have to go for a simple manual merge with some kind of collision check and handling:

hash2.each_key do |key|
  if ( hash1.has_key?(key) )
       hash1[ "hash2-originated-#{key}" ] = hash2[key]
  else
       hash1[key]=hash2[key]
  end
end

Obviously, this is very rudimentary and assumes that hash1 doesn't have any keys called "hash2-originated-whatever" - you may be better off just adding a number to the key so it becomes key1, key2 and so on until you hit on one that isn't already in hash1. Also, I haven't done any ruby for a few months so that's probably not syntactically correct, but you should be able to get the gist.

Alternatively redefine the value of the key as an array so that hash1[key] returns the original value from hash1 and the value from hash2. Depends what you want your outcome to be really.

2
  • How about if not keeping both key, but adding up the values of the same key? May 28, 2015 at 5:19
  • 1
    @TomK.C.Chiu That would very much depend on circumstances that we can't judge from the question - what if the values in hash1 are strings and hash2 are integers? For some cases that might be a viable option, but more often it would cause problems- the suggestion of using lists for values works around this quite cleanly.
    – glenatron
    May 28, 2015 at 9:03
1

Here you can merge your 2 hash by reverse_merge

order = {
 id: 33987,
 platform: 'web'
}

user = {
  name: 'Jhon Doe',
  email: '[email protected]' 
}
newHash = order.reverse_merge!(user)
render json: { data: newHash, status: 200 }

# => {:name=>"Jhon Doe", :email=>"[email protected]", :id=>33987, :platform=>"web"}
1
  • What did you do to make this work? I tried this on ruby 3.2.2, {} responds to neither reverse_merge nor reverse_merge!, and your code doesn't seem to include ActiveSupport
    – Pelle
    Oct 17, 2023 at 10:35
0

If you want to merge the two hashes options and defaults without overwriting the destination hash, you may check with select if the key is already present in the destination hash. Here's the pure Ruby solution without Rails:

options  = { "a" => 100, "b" => 200 }
defaults = { "b" => 254, "c" => 300 }
options.merge!(defaults.select{ |k,_| not options.has_key? k })

# output
# => {"a"=>100, "b"=>200, "c"=>300}

Or if the key is present, but contains nil and you want to overwrite it:

options.merge!(defaults.select{ |k,_| options[k].nil? })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.