导轨 <4.0“尝试"方法抛出 NoMethodError? [英] rails &lt; 4.0 &quot;try&quot; method throwing NoMethodError?

查看:38
本文介绍了导轨 <4.0“尝试"方法抛出 NoMethodError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么尝试抛出错误?这不是违背了整个目的吗?也许它只是在控制台中?

Why is try throwing an error? Doesnt that defeat the whole purpose? Maybe its just in the console?

ruby-1.9.2-p180 :101 > User.first.try(:something)
NoMethodError: undefined method `something' for #<User:0x000001046ad128>
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in `method_missing'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.10/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from (irb):101
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

谢谢各位,现在我明白了.

Thanks guys, now I get it.

有没有办法在不使用 respond_to 的情况下做我想做的事?,这样 User.try(:something) 返回 nil而不是抛出错误?

Is there a way to do what I wanted without doing using respond_to?, such that User.try(:something) returns nil instead of throwing the error?

推荐答案

Rails 3

您误解了 try 的工作原理,来自 精美手册:

Rails 3

You misunderstand how try works, from the fine manual:

尝试(*a, &b)
调用由符号方法标识的方法,将任何参数和/或指定的块传递给它,就像常规的 Ruby Object#send 一样.

然而,与该方法不同的是,如果接收对象是 nil,则不会引发 NoMethodError 异常,而是返回 nil对象或 NilClass.

Unlike that method however, a NoMethodError exception will not be raised and nil will be returned instead, if the receiving object is a nil object or NilClass.

try 的版本是修补为 NilClass:

And the version of try that is patched into NilClass:

try(*args)
nil 上调用 try 总是返回 nil.

try(*args)
Calling try on nil always returns nil.

所以 try 不会忽略您在对象上调用不存在的方法的尝试,它会忽略您在 nil 上调用方法的尝试并返回 nil 而不是引发异常.try 方法只是一种简单的方法,可以避免在方法调用链的每一步都检查 nil.

So try doesn't ignore your attempt to call a non-existent method on an object, it ignores your attempt to call a method on nil and returns nil instead of raising an exception. The try method is just an easy way to avoid having to check for nil at every step in a chain of method calls.

try 的行为在 Rails 4 中发生了变化,所以现在它:

The behavior of try has changed in Rails 4 so now it:

调用名称作为第一个参数的公共方法,就像 public_send 一样,除了如果接收者没有响应它,调用返回 nil 而不是引发一个例外.

Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.

所以现在 try 会同时处理这两项检查.如果你想要 Rails 3 的行为,有 try!:

So now try takes care of both checks at once. If you want the Rails 3 behavior, there is try!:

try 相同,但如果接收 [sic] 不是 nil 且未实现 [sic],则会引发 NoMethodError 异常尝试过的方法.

Same as try, but will raise a NoMethodError exception if the receiving [sic] is not nil and does not implemented [sic] the tried method.

这篇关于导轨 <4.0“尝试"方法抛出 NoMethodError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆