aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorCorey Ward <corey.atx@gmail.com>2013-11-02 17:03:43 -0500
committerCorey Ward <corey.atx@gmail.com>2013-12-09 13:50:18 -0600
commit0438134476b05d14dffbb28f84eac582bd3cfa8f (patch)
tree97d3f91cff3fe7f3e351d3e1b2305f42f932f5bd /activesupport/lib/active_support
parent4aae538d9ffff3a00a81f3da52fa70f7fd79ac74 (diff)
downloadrails-0438134476b05d14dffbb28f84eac582bd3cfa8f.tar.gz
rails-0438134476b05d14dffbb28f84eac582bd3cfa8f.tar.bz2
rails-0438134476b05d14dffbb28f84eac582bd3cfa8f.zip
Fix Hash#deep_merge bug and improve documentation — resolves #12738
Previously merging into a hash with a falsy value would not result in the merge-block being called. The fix is simply to check for presence of the key in the hash. The documentation example for `deep_merge` now appropriately demonstrates what a deep merge does.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/hash/deep_merge.rb32
1 files changed, 21 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
index dc86c92003..b53fa524c6 100644
--- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
+++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
@@ -1,25 +1,35 @@
class Hash
# Returns a new hash with +self+ and +other_hash+ merged recursively.
#
- # h1 = { x: { y: [4, 5, 6] }, z: [7, 8, 9] }
- # h2 = { x: { y: [7, 8, 9] }, z: 'xyz' }
+ # h1 = { a: true, b: { c: [1,2,3] } }
+ # h2 = { a: false, b: { x: [3,4,5] } }
#
- # h1.deep_merge(h2) # => {x: {y: [7, 8, 9]}, z: "xyz"}
- # h2.deep_merge(h1) # => {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
- # h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
- # # => {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
+ # h1.deep_merge(h2) #=> { a: false, b: { c: [1,2,3], x: [3,4,5] } }
+ #
+ # Like with Hash#merge in the standard library, a block can be provided
+ # to merge values:
+ #
+ # h1 = { a: 100, b: 200, c: { c1: 100 } }
+ # h2 = { b: 250, c: { c1: 200 } }
+ # h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
+ # # => { a: 100, b: 450, c: { c1: 300 } }
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end
# Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash, &block)
- other_hash.each_pair do |k,v|
- tv = self[k]
- if tv.is_a?(Hash) && v.is_a?(Hash)
- self[k] = tv.deep_merge(v, &block)
+ other_hash.each_pair do |current_key, other_value|
+ this_value = self[current_key]
+
+ self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
+ this_value.deep_merge(other_value, &block)
else
- self[k] = block && tv ? block.call(k, tv, v) : v
+ if block_given? && key?(current_key)
+ block.call(current_key, this_value, other_value)
+ else
+ other_value
+ end
end
end
self