diff options
author | Bradford Folkens <bfolkens@gmail.com> | 2009-03-12 15:03:01 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-03-12 15:03:01 +0000 |
commit | aa57e66fec3a131f5d246b8950a2c3286f858b78 (patch) | |
tree | 2a697e042513154e149133f34b0a0ef13337aab1 /activesupport | |
parent | 8a17fd1a65ab8e2fa6b36d79603fde0e6ffd083f (diff) | |
download | rails-aa57e66fec3a131f5d246b8950a2c3286f858b78.tar.gz rails-aa57e66fec3a131f5d246b8950a2c3286f858b78.tar.bz2 rails-aa57e66fec3a131f5d246b8950a2c3286f858b78.zip |
Ensure HWIA#reverse_merge! retrurns HWIA [#421 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/indifferent_access.rb | 6 | ||||
-rw-r--r-- | 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 c96c5160b3..34ba8a005d 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) + super other_hash.with_indifferent_access + 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 80582cd9c9..482ae57830 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 |