Created
October 31, 2014 16:50
-
-
Save exAspArk/d81e5d7b6ad1bdd3bcf6 to your computer and use it in GitHub Desktop.
Immutable deep hash merge (ruby)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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