From 54575d87973b95cbd278fbe086b70761584d286d Mon Sep 17 00:00:00 2001 From: Pranas Kiziela Date: Thu, 13 Sep 2012 21:49:49 +0300 Subject: Allow passing block to deep_merge and deep_merge! Hash#merge accepts block that you can use to customize how hash values are merged. This change makes merge and deep_merge compatible. --- activesupport/test/core_ext/hash_ext_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 94463cc311..01934dd2c3 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -582,6 +582,16 @@ class HashExtTest < ActiveSupport::TestCase assert_equal expected, hash_1 end + def test_deep_merge_with_block + hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } } + hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } } + expected = { :a => [:a, "a", 1], :b => "b", :c => { :c1 => [:c1, "c1", 2], :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } } + assert_equal(expected, hash_1.deep_merge(hash_2) { |k,o,n| [k, o, n] }) + + hash_1.deep_merge!(hash_2) { |k,o,n| [k, o, n] } + assert_equal expected, hash_1 + end + def test_deep_merge_on_indifferent_access hash_1 = HashWithIndifferentAccess.new({ :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }) hash_2 = HashWithIndifferentAccess.new({ :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }) -- cgit v1.2.3