From 8c07696f470cff823ad0b538ca4bea1594742580 Mon Sep 17 00:00:00 2001 From: Chris Zetter Date: Wed, 30 Nov 2011 15:04:35 +0000 Subject: [PATCH] Fix lookup on HashWithIndifferentAccess for array values. --- .../lib/active_support/hash_with_indifferent_access.rb | 3 ++- activesupport/test/core_ext/hash_ext_test.rb | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/hash_with_indifferent_access.rb b/activesupport/lib/active_support/hash_with_indifferent_access.rb index 6e1c0da991..bb47b3560a 100644 --- a/activesupport/lib/active_support/hash_with_indifferent_access.rb +++ b/activesupport/lib/active_support/hash_with_indifferent_access.rb @@ -164,7 +164,8 @@ def convert_value(value) if value.is_a? Hash value.nested_under_indifferent_access elsif value.is_a?(Array) - value.dup.replace(value.map { |e| convert_value(e) }) + value = value.dup if value.frozen? + value.replace(value.map { |e| convert_value(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 5d422ce5ad..4dc9f57038 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -457,6 +457,13 @@ def test_indifferent_to_hash assert_equal '1234', roundtrip.default end + def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_access + hash = HashWithIndifferentAccess.new {|h, k| h[k] = []} + hash[:a] << 1 + + assert_equal [1], hash[:a] + end + def test_indifferent_hash_with_array_of_hashes hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access assert_equal "1", hash[:urls][:url].first[:address]