Permalink
Browse files

Fix merge and dup for hashes with indifferent access (closes #3404) […

…kenneth.miller@bitfield.net]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3468 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 010e999 commit 9c24899ab0fad8baa4db543dd45f3d20ae95d3b3 @dhh dhh committed Jan 23, 2006
View
@@ -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.]
@@ -41,6 +41,14 @@ def values_at(*indices)
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
@@ -116,6 +116,25 @@ def test_indifferent_update
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 ])

0 comments on commit 9c24899

Please sign in to comment.