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