Skip to content

Commit

Permalink
Fix lookup on HashWithIndifferentAccess for array values.
Browse files Browse the repository at this point in the history
  • Loading branch information
zetter authored and carlosantoniodasilva committed Jun 19, 2012
1 parent 32c65d8 commit 8c07696
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -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]
Expand Down

0 comments on commit 8c07696

Please sign in to comment.