Skip to content

Instantly share code, notes, and snippets.

@ericdke
Created March 28, 2014 10:59
Show Gist options
  • Save ericdke/9830152 to your computer and use it in GitHub Desktop.
Save ericdke/9830152 to your computer and use it in GitHub Desktop.
Deep merge hashes
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