Skip to content

Instantly share code, notes, and snippets.

@exAspArk
Created October 31, 2014 16:50
Show Gist options
  • Select an option

  • Save exAspArk/d81e5d7b6ad1bdd3bcf6 to your computer and use it in GitHub Desktop.

Select an option

Save exAspArk/d81e5d7b6ad1bdd3bcf6 to your computer and use it in GitHub Desktop.
Immutable deep hash merge (ruby)
def deep_merge(hash1, hash2)
result = hash1.dup
hash2.keys.each do |key|
if hash2[key].is_a?(Hash) && hash1[key].is_a?(Hash)
result[key] = deep_merge(hash1[key], hash2[key])
else
result[key] = hash2[key]
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment