Created
March 28, 2014 10:59
-
-
Save ericdke/9830152 to your computer and use it in GitHub Desktop.
Deep merge hashes
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(h1, h2) | |
h1.merge(h2) { |key, h1_elem, h2_elem| deep_merge(h1_elem, h2_elem) } | |
end | |
# Usage | |
wish_list = { | |
8 => { | |
title: "The Color of Magic", | |
}, | |
42 => { | |
title: "The Hitch-Hiker's Guide to the Galaxy", | |
price: 5 | |
} | |
} | |
basket = { | |
8 => { | |
price: 10 | |
}, | |
1729 => { | |
title: "Ramanujan: Twelve Lectures on Subjects Suggested by His Life and Work", | |
price: 28 | |
} | |
} | |
deep_merge(basket, wish_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment