aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb6
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb6
3 files changed, 11 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 3aac59931f..947828064b 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -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]
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index a9169cf4a5..4d32a6b8a5 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -24,7 +24,7 @@ module ActiveSupport #:nodoc:
# 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 @@ module ActiveSupport #:nodoc:
# 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
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 5691ec7c1c..359eacaba3 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -5,6 +5,7 @@ class HashExtTest < Test::Unit::TestCase
@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 @@ class HashExtTest < Test::Unit::TestCase
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