提交 43fdbd5e 编写于 作者: J Jeremy Kemper

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
上级 b72763a9
*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]
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册