aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-12-20 22:28:47 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-12-20 22:28:47 +0000
commit43fdbd5e1ff48f1b55963a6bf3fbbdd3f90f0d67 (patch)
treefc50e7b004999dc12d3693e1c5e8ed1520a8962b
parentb72763a96f0c936699d0f211be553f8f75accd07 (diff)
downloadrails-43fdbd5e1ff48f1b55963a6bf3fbbdd3f90f0d67.tar.gz
rails-43fdbd5e1ff48f1b55963a6bf3fbbdd3f90f0d67.tar.bz2
rails-43fdbd5e1ff48f1b55963a6bf3fbbdd3f90f0d67.zip
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
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb2
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb12
3 files changed, 12 insertions, 4 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 878f44bbd7..7252262d6a 100644
--- a/activesupport/CHANGELOG
+++ b/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]
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index 2bd4138be4..f104989df5 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 || key] = value
+ options[(key.to_sym rescue key) || key] = value
options
end
end
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 4d01faa5d2..a39c32a2e3 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -6,6 +6,11 @@ class HashExtTest < Test::Unit::TestCase
@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 @@ class HashExtTest < Test::Unit::TestCase
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