未定义的方法'>'对于nil:NilClass< NoMethodError> [英] Undefined method '>' for nil:NilClass <NoMethodError>

查看:81
本文介绍了未定义的方法'>'对于nil:NilClass< NoMethodError>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有以下代码

 def update_state_actions
    states.each do |state|
      @state_turns[state.id] -= 1 if @state_turns[state.id] > 0 && state.auto_removal_timing == 1
    end
  end

现在在...的行

 @state_turns[state.id] -= 1 if @state_turns[state.id] > 0 && state.auto_removal_timing == 1

显示错误

in 'block update_state_actions' : Undefined method '>' for nil:NilClass <NoMethodError>

该错误的原因是什么? 为什么>被认为是一种方法,但它是逻辑运算符?

what is the cause of the error? how come > is considered as a method but it is a logical operator?

推荐答案

为什么>被认为是方法,但它是逻辑运算符?

how come > is considered as a method but it is a logical operator?

没有问题.在Ruby中,当您编写类似1 + 2的表达式时,在内部将其理解为1.+( 2 ):在接收方1上调用方法#+,其中2作为单个参数.理解这一点的另一种方法是,您将消息[ :+, 2 ]发送到对象1.

There is no problem with that. In Ruby, when you write an expression like 1 + 2, internally it is understood as 1.+( 2 ): Calling method #+ on the receiver 1 with 2 as a single argument. Another way to understand the same is, that you are sending the message [ :+, 2 ] to the object 1.

该错误的原因是什么?

what is the cause of the error?

在您的情况下,@state_turns[ state.id ]由于某种原因返回nil.因此,表达式@state_turns[state.id] > 0变为nil > 0,正如我之前所说,它被理解为在nil上调用#>方法.但是您可以检查nil所属的NilClass上是否没有定义实例方法#>:

Now in your case, @state_turns[ state.id ] returns nil for some reason. So the expression @state_turns[state.id] > 0 becomes nil > 0, which, as I said earlier, is understood as calling #> method on nil. But you can check that NilClass, to which nil belongs, has no instance method #> defined on it:

NilClass.instance_methods.include? :> # => false
nil.respond_to? :> # => false

因此,NoMethodError异常是合法错误.通过引发此错误,Ruby可以保护您:它可以尽早告诉您@state_turns[ state.id ]并不是您想像的那样.这样,您可以更早地纠正错误,并成为一个更有效率的程序员.另外,可以使用begin ... rescue ... end语句挽救Ruby异常. Ruby异常通常是非常友好和有用的对象,您应该学习如何在软件项目中定义自定义异常.

The NoMethodError exception is therefore a legitimate error. By raising this error, Ruby protects you: It tells you early that your @state_turns[ state.id ] is not what you assume it to be. That way, you can correct your errors earlier, and be a more efficient programmer. Also, Ruby exceptions can be rescued with begin ... rescue ... end statement. Ruby exceptions are generally very friendly and useful objects, and you should learn how to define your custom exceptions in your software projects.

为了进一步扩展讨论范围,让我们看一下错误的出处.当编写类似nil > 10的表达式(实际上是nil.>( 10 ))时,Ruby开始在nil的查找链中搜索#>方法.您可以通过键入以下内容查看查找链:

To extend this discussion a bit more, let's look at from where your error is coming. When you write an expression like nil > 10, which is actually nil.>( 10 ), Ruby starts searching for #> method in the lookup chain of nil. You can see the lookup chain by typing:

    nil.singleton_class.ancestors #=> [NilClass, Object, Kernel, BasicObject]

将在祖先链的每个模块中搜索该方法:首先,Ruby将检查是否在NilClass上定义了#>,然后在Object上定义了,然后在Kernel上定义了,最后在BasicObject上定义了.如果在任何一个中都没有找到#>,Ruby将继续尝试使用method_missing方法,以在查找链的所有模块上再次按顺序进行.如果甚至method_missing都不处理:>消息,则会引发NoMethodError异常.为了演示,让我们在Object中定义#method_missing方法,方法是插入一条自定义消息,该消息将代替NoMethodError出现:

The method will be searched in each module of the ancestor chain: First, Ruby will check whether #> is defined on NilClass, then on Object, then Kernel, and finally, BasicObject. If #> is not found in any of them, Ruby will continue by trying method_missing methods, again in order on all the modules of the lookup chain. If even method_missing does not handle the :> message, NoMethodError exception will be raised. To demonstrate, let's define #method_missing method in Object by inserting a custom message, that will appear instead of NoMethodError:

class Object
  def method_missing( name, *args )
    puts "There is no method '##{name}' defined on #{self.class}, you dummy!"
  end
end

[ 1, 2, 3 ][ 3 ] > 2 
#=> There is no method '#>' defined on NilClass, you dummy!

为什么不像NullPointerException那样显示

Why doesn't it says like NullPointerException

在Ruby中没有这样的例外.检查Ruby的 Exception 类.

There is no such exception in Ruby. Check the Ruby's Exception class.

这篇关于未定义的方法'&gt;'对于nil:NilClass&lt; NoMethodError&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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