Skip to content
Browse files

Only convert direct hash instances in hash with indifferent access.

  • Loading branch information...
1 parent 7e903a3 commit 6e66d6100b1d1628fd602a77406401a90e661ea7 @josevalim josevalim committed Nov 26, 2010
View
7 activesupport/lib/active_support/hash_with_indifferent_access.rb
@@ -138,11 +138,10 @@ def convert_key(key)
end
def convert_value(value)
- case value
- when Hash
+ if value.class == Hash
self.class.new_from_hash_copying_default(value)
- when Array
- value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }
+ elsif value.is_a?(Array)
+ value.dup.replace(value.map { |e| convert_value(e) })
else
value
end
View
14 activesupport/test/core_ext/hash_ext_test.rb
@@ -6,6 +6,15 @@
require 'active_support/core_ext/object/conversions'
class HashExtTest < Test::Unit::TestCase
+ class IndifferentHash < HashWithIndifferentAccess
+ end
+
+ class SubclassingArray < Array
+ end
+
+ class SubclassingHash < Hash
+ end
+
def setup
@strings = { 'a' => 1, 'b' => 2 }
@symbols = { :a => 1, :b => 2 }
@@ -99,6 +108,11 @@ def test_stringify_keys_bang_for_hash_with_indifferent_access
assert_equal @strings, @mixed.with_indifferent_access.dup.stringify_keys!
end
+ def test_hash_subclass
+ flash = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
+ assert_kind_of SubclassingHash, flash["foo"]
+ end
+
def test_indifferent_assorted
@strings = @strings.with_indifferent_access
@symbols = @symbols.with_indifferent_access

0 comments on commit 6e66d61

Please sign in to comment.
Something went wrong with that request. Please try again.