Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hash#symbolize_keys skips keys that can't be symbolized. Closes #10500.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8454 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 43fdbd5e1ff48f1b55963a6bf3fbbdd3f90f0d67 1 parent b72763a
@jeremy jeremy authored
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Hash#symbolize_keys skips keys that can't be symbolized. #10500 [Brad Greenlee]
+
* Ruby 1.9 compatibility. #1689, #10466, #10468, #10554 [Cheah Chu Yeow, Pratik Naik, Jeremy Kemper, Dirkjan Bussink]
* TimeZone#to_s uses UTC rather than GMT. #1689 [Cheah Chu Yeow]
View
2  activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -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 || key] = value
+ options[(key.to_sym rescue key) || key] = value
options
end
end
View
12 activesupport/test/core_ext/hash_ext_test.rb
@@ -6,6 +6,11 @@ def setup
@symbols = { :a => 1, :b => 2 }
@mixed = { :a => 1, 'b' => 2 }
@fixnums = { 0 => 1, 1 => 2 }
+ if RUBY_VERSION < '1.9.0'
+ @illegal_symbols = { "\0" => 1, "" => 2, [] => 3 }
+ else
+ @illegal_symbols = { [] => 3 }
+ end
end
def test_methods
@@ -22,16 +27,17 @@ def test_symbolize_keys
assert_equal @symbols, @symbols.symbolize_keys
assert_equal @symbols, @strings.symbolize_keys
assert_equal @symbols, @mixed.symbolize_keys
-
- assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
end
def test_symbolize_keys!
assert_equal @symbols, @symbols.dup.symbolize_keys!
assert_equal @symbols, @strings.dup.symbolize_keys!
assert_equal @symbols, @mixed.dup.symbolize_keys!
+ end
- assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
+ def test_symbolize_keys_preserves_keys_that_cant_be_symbolized
+ assert_equal @illegal_symbols, @illegal_symbols.symbolize_keys
+ assert_equal @illegal_symbols, @illegal_symbols.dup.symbolize_keys!
end
def test_symbolize_keys_preserves_fixnum_keys
Please sign in to comment.
Something went wrong with that request. Please try again.