From 93c34a710f7befe50ac145e4951f91a6d2d8ab18 Mon Sep 17 00:00:00 2001 From: Larry Sprock Date: Sat, 13 Nov 2010 23:01:14 -0800 Subject: [PATCH] HashWithIndifferentAccess should not change the subclass of an array --- .../active_support/hash_with_indifferent_access.rb | 2 +- activesupport/test/core_ext/hash_ext_test.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 06dd189..320f5c1 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -144,7 +144,7 @@ module ActiveSupport when 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 } + value.dup.replace(value.collect { |e| e.is_a?(Hash) ? self.class.new_from_hash_copying_default(e) : e }) else value end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index d7b77e4..370f26b 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -8,6 +8,9 @@ require 'active_support/core_ext/object/conversions' class HashExtTest < Test::Unit::TestCase class IndifferentHash < HashWithIndifferentAccess end + + class SubclassingArray < Array + end def setup @strings = { 'a' => 1, 'b' => 2 } @@ -251,6 +254,20 @@ class HashExtTest < Test::Unit::TestCase hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access assert_equal "1", hash[:urls][:url].first[:address] end + + def test_should_preserve_array_subclass_when_value_is_array + array = SubclassingArray.new + array << { "address" => "1" } + hash = { "urls" => { "url" => array }}.with_indifferent_access + assert_equal SubclassingArray, hash[:urls][:url].class + end + + def test_should_preserve_array_class_when_hash_value_is_frozen_array + array = SubclassingArray.new + array << { "address" => "1" } + hash = { "urls" => { "url" => array.freeze }}.with_indifferent_access + assert_equal SubclassingArray, hash[:urls][:url].class + end def test_stringify_and_symbolize_keys_on_indifferent_preserves_hash h = HashWithIndifferentAccess.new -- 1.7.3.1