aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/indifferent_access.rb8
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb19
3 files changed, 29 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 2f22573f28..1d517d8862 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix merge and dup for hashes with indifferent access #3404 [kenneth.miller@bitfield.net]
+
* Fix the requires in option_merger_test to unbreak AS tests. [Sam Stephenson]
* Make HashWithIndifferentAccess#update behave like Hash#update by returning the hash. #3419, #3425 [asnem@student.ethz.ch, JanPrill@blauton.de, Marcel Molina Jr.]
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 458f8961a3..c96e63b169 100644
--- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
+++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
@@ -41,6 +41,14 @@ class HashWithIndifferentAccess < Hash
indices.collect {|key| self[convert_key(key)]}
end
+ def dup
+ HashWithIndifferentAccess.new(self)
+ end
+
+ def merge(hash)
+ self.dup.update(hash)
+ end
+
protected
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index a62dc3fbca..d74cea0991 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -116,6 +116,25 @@ class HashExtTest < Test::Unit::TestCase
assert [updated_with_strings, updated_with_symbols, updated_with_mixed].all? {|hash| hash.keys.size == 2}
end
+ def test_indifferent_merging
+ hash = HashWithIndifferentAccess.new
+ hash[:a] = 'failure'
+ hash['b'] = 'failure'
+
+ other = { 'a' => 1, :b => 2 }
+
+ merged = hash.merge(other)
+
+ assert_equal HashWithIndifferentAccess, merged.class
+ assert_equal 1, merged[:a]
+ assert_equal 2, merged['b']
+
+ hash.update(other)
+
+ assert_equal 1, hash[:a]
+ assert_equal 2, hash['b']
+ end
+
def test_assert_valid_keys
assert_nothing_raised do
{ :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])