Skip to content

Commit

Permalink
fix HashWithIndifferentAccess#to_hash behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulnsward authored and rafaelfranca committed Jun 4, 2013
1 parent 77ac25c commit 81c7c9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions activesupport/lib/active_support/hash_with_indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,21 @@ def to_options!; self end

# Convert to a regular hash with string keys.
def to_hash
Hash.new(default).merge!(self)
_new_hash= {}
each do |key, value|
_new_hash[convert_key(key)] = convert_value(value,true)
end
Hash.new(default).merge!(_new_hash)
end

protected
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end

def convert_value(value)
def convert_value(value, _convert_for_to_hash = false)
if value.is_a? Hash
value.nested_under_indifferent_access
_convert_for_to_hash ? value.to_hash : value.nested_under_indifferent_access
elsif value.is_a?(Array)
value = value.dup if value.frozen?
value.map! { |e| convert_value(e) }
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def test_indifferent_to_hash
roundtrip = mixed_with_default.with_indifferent_access.to_hash
assert_equal @strings, roundtrip
assert_equal '1234', roundtrip.default
new_to_hash = @nested_mixed.with_indifferent_access.to_hash
assert_not new_to_hash.instance_of?(HashWithIndifferentAccess)
assert_not new_to_hash["a"].instance_of?(HashWithIndifferentAccess)
assert_not new_to_hash["a"]["b"].instance_of?(HashWithIndifferentAccess)
end

def test_lookup_returns_the_same_object_that_is_stored_in_hash_indifferent_access
Expand Down

0 comments on commit 81c7c9c

Please sign in to comment.