diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-08-17 15:08:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-17 15:08:58 -0400 |
commit | 9876196612bcd59faadca7f091de859fdfe6b991 (patch) | |
tree | 0c739ad3c4f4dac481224de519f2b3125f351824 /activesupport | |
parent | 835246e622dc0431af3cb951db22ef78876006af (diff) | |
parent | 2eacb1c30eac4058b8412527f9bda6e7ba6103d4 (diff) | |
download | rails-9876196612bcd59faadca7f091de859fdfe6b991.tar.gz rails-9876196612bcd59faadca7f091de859fdfe6b991.tar.bz2 rails-9876196612bcd59faadca7f091de859fdfe6b991.zip |
Merge pull request #30275 from msimonborg/deep_merge_patch
Faster and more readable implementation of Hash#deep_merge
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/deep_merge.rb | 18 |
1 files changed, 6 insertions, 12 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 f92a2f1c2a..9bc50b7bc6 100644 --- a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -21,20 +21,14 @@ class Hash # Same as +deep_merge+, but modifies +self+. def deep_merge!(other_hash, &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) + merge!(other_hash) do |key, this_val, other_val| + if this_val.is_a?(Hash) && other_val.is_a?(Hash) + this_val.deep_merge(other_val, &block) + elsif block_given? + block.call(key, this_val, other_val) else - if block_given? && key?(current_key) - block.call(current_key, this_value, other_value) - else - other_value - end + other_val end end - - self end end |