aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Graham <dgraham@github.com>2012-10-16 13:05:25 -0600
committerDavid Graham <dgraham@github.com>2012-10-26 11:36:35 -0600
commitf38e752bdf27668faf1125479f815832484c0a72 (patch)
treeece0c6c0c6959edcba2e8fed61aea84dc8a7b60c /activesupport
parent6ac33f9e4b4dbfe34f9884d7b262a7ba2c3356e1 (diff)
downloadrails-f38e752bdf27668faf1125479f815832484c0a72.tar.gz
rails-f38e752bdf27668faf1125479f815832484c0a72.tar.bz2
rails-f38e752bdf27668faf1125479f815832484c0a72.zip
Implement replace method so key? works correctly.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md2
-rw-r--r--activesupport/lib/active_support/hash_with_indifferent_access.rb8
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb12
3 files changed, 22 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 23e2ce0b03..4b06210c4a 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Implement HashWithIndifferentAccess#replace so key? works correctly. *David Graham*
+
* Hash#extract! returns only those keys that present in the receiver.
{:a => 1, :b => 2}.extract!(:a, :x) # => {:a => 1}
diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb
index 0c78f1611f..306d80b2df 100644
--- a/activesupport/lib/active_support/hash_with_indifferent_access.rb
+++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -204,6 +204,14 @@ module ActiveSupport
replace(reverse_merge( other_hash ))
end
+ # Replaces the contents of this hash with other_hash.
+ #
+ # h = { "a" => 100, "b" => 200 }
+ # h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}
+ def replace(other_hash)
+ super(self.class.new_from_hash_copying_default(other_hash))
+ end
+
# Removes the 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 7cfe7b0ea7..6746b58cd3 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -428,6 +428,18 @@ class HashExtTest < ActiveSupport::TestCase
assert_equal 2, hash['b']
end
+ def test_indifferent_replace
+ hash = HashWithIndifferentAccess.new
+ hash[:a] = 42
+
+ replaced = hash.replace(b: 12)
+
+ assert hash.key?('b')
+ assert !hash.key?(:a)
+ assert_equal 12, hash[:b]
+ assert_same hash, replaced
+ end
+
def test_indifferent_merging_with_block
hash = HashWithIndifferentAccess.new
hash[:a] = 1