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
This commit is contained in:
Jeremy Kemper 2007-12-20 22:28:47 +00:00
parent b72763a96f
commit 43fdbd5e1f
3 changed files with 12 additions and 4 deletions

View File

@ -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 File

@ -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

View File

@ -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