How can I merge two hashes without overwritten duplicate keys in Ruby?

Ruby

Ruby Problem Overview


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.

Ruby Solutions


Solution 1 - Ruby

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)

Solution 2 - Ruby

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 }

Solution 3 - Ruby

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.

Solution 4 - Ruby

Here you can merge your 2 hash by reverse_merge

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

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

Solution 5 - Ruby

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? })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClaudio AcciaresiView Question on Stackoverflow
Solution 1 - RubyAlex ReisnerView Answer on Stackoverflow
Solution 2 - RubyujifgcView Answer on Stackoverflow
Solution 3 - RubyglenatronView Answer on Stackoverflow
Solution 4 - RubyDaniel Agus SidabutarView Answer on Stackoverflow
Solution 5 - RubyChristianView Answer on Stackoverflow