我刚刚从 Rails 3.1.0 升级到 3.2.3。一旦我清除了我的 gem 依赖问题,我就设法运行测试,我发现了这一点:
1.9.2p180 :005 > Rails.version
=> "3.2.3"
1.9.2p180 :006 > a = 'test'
=> "test"
1.9.2p180 :007 > a.try(:banana)
NoMethodError: undefined method `banana' for "test":String
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180@app_tier/gems/activesupport-3.2.3/lib /active_support/core_ext/object/try.rb:32:in `try'
from (irb):7
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180@app_tier/gems/railties-3.2.3/lib/rails /commands/console.rb:47:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180@app_tier/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/jamesthullbery/.rvm/gems/ruby-1.9.2-p180@app_tier/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.2p180 :008 >
当我在 Rails 3.1.0 上执行相同的命令时,我没有遇到任何问题:
1.9.2p180 :004 > Rails.version
=> "3.1.0"
1.9.2p180 :005 > a = 'test'
=> "test"
1.9.2p180 :006 > a.try(:banana)
=> nil
1.9.2p180 :007 >
有没有人看到这个问题?当然,我的环境有问题,而不是 Rails 框架。好像很难准确的搜索到try的方法,所以网上还没找到。还有人看到这个吗?谢谢!