From 3326e0379310ec146e7390c4993111e7aa015e3f Mon Sep 17 00:00:00 2001 From: Bradford Folkens Date: Fri, 9 Jan 2009 10:20:37 -0600 Subject: [PATCH] fixing bug involving unexpected behavior with HashWithIndifferentAcess#reverse_merge! --- .../core_ext/hash/indifferent_access.rb | 6 ++++++ activesupport/test/core_ext/hash_ext_test.rb | 7 +++++++ 2 files changed, 13 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index c96c516..6a1b2fd 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -91,6 +91,12 @@ class HashWithIndifferentAccess < Hash self.dup.update(hash) end + # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. + # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess. + def reverse_merge(other_hash) + self.class.new(other_hash).merge(self) + end + # Removes a specified key from the hash. def delete(key) super(convert_key(key)) diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index b63ab30..09bd700 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -174,6 +174,13 @@ class HashExtTest < Test::Unit::TestCase assert_equal 2, hash['b'] end + def test_indifferent_reverse_merging + hash = HashWithIndifferentAccess.new( 'some' => 'value', 'other' => 'value' ) + hash.reverse_merge!( :some => 'noclobber', :another => 'clobber' ) + assert_equal 'value' , hash[:some] + assert_equal 'clobber', hash[:another] + end + def test_indifferent_deleting get_hash = proc{ { :a => 'foo' }.with_indifferent_access } hash = get_hash.call -- 1.5.6.3 From eedfed9efb0bc47f467c9901ca4761268ad767e0 Mon Sep 17 00:00:00 2001 From: Dmitry Ratnikov Date: Fri, 16 Jan 2009 14:23:42 -0600 Subject: [PATCH] Fixed to used underlying implementation of reverse_merge rather than dublicating it. --- .../core_ext/hash/indifferent_access.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 6a1b2fd..34ba8a0 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -94,7 +94,7 @@ class HashWithIndifferentAccess < Hash # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess. def reverse_merge(other_hash) - self.class.new(other_hash).merge(self) + super other_hash.with_indifferent_access end # Removes a specified key from the hash. -- 1.5.6.3