Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hash#symbolize_keys behaves well with integer keys. Closes #9890.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7945 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit cfffedb4d86bcf3190da5e6433b694fc2e9f1bd9 1 parent a2172e7
Jeremy Kemper jeremy authored
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Hash#symbolize_keys behaves well with integer keys. #9890 [PotatoSalad]
+
* Multibyte: String#slice supports regexp argument. #9646 [yob]
* object.duplicable? returns true if object.dup is safe. False for nil, true, false, symbols, and numbers; true otherwise. #9333 [sur]
6 activesupport/lib/active_support/core_ext/hash/keys.rb
View
@@ -24,7 +24,7 @@ def stringify_keys!
# Return a new hash with all keys converted to symbols.
def symbolize_keys
inject({}) do |options, (key, value)|
- options[key.to_sym] = value
+ options[key.to_sym || key] = value
options
end
end
@@ -32,8 +32,8 @@ def symbolize_keys
# Destructively convert all keys to symbols.
def symbolize_keys!
keys.each do |key|
- unless key.is_a?(Symbol)
- self[key.to_sym] = self[key]
+ unless key.is_a?(Symbol) || (new_key = key.to_sym).nil?
+ self[new_key] = self[key]
delete(key)
end
end
6 activesupport/test/core_ext/hash_ext_test.rb
View
@@ -5,6 +5,7 @@ def setup
@strings = { 'a' => 1, 'b' => 2 }
@symbols = { :a => 1, :b => 2 }
@mixed = { :a => 1, 'b' => 2 }
+ @fixnums = { 0 => 1, 1 => 2 }
end
def test_methods
@@ -33,6 +34,11 @@ def test_symbolize_keys!
assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
end
+ def test_symbolize_keys_preserves_fixnum_keys
+ assert_equal @fixnums, @fixnums.symbolize_keys
+ assert_equal @fixnums, @fixnums.dup.symbolize_keys!
+ end
+
def test_stringify_keys
assert_equal @strings, @symbols.stringify_keys
assert_equal @strings, @strings.stringify_keys
Please sign in to comment.
Something went wrong with that request. Please try again.