简体   繁体   English

nil:NilClass的未定义方法“ +”(NoMethodError)

[英]Undefined method `+' for nil:NilClass (NoMethodError)

I'm writing a simple class that is initialized with a variable called "cash" which is an integer. 我正在编写一个简单的类,该类使用名为“ cash”的变量初始化,该变量是整数。

Below is the code. 下面是代码。 When I run this, I get the NoMethodError. 运行此命令时,出现NoMethodError。 I know I can easily fix this by referencing the local class variable with @cash, but a book I read on OOP recommend to almost never use the @, and instead set the attr and use simply 'cash'. 我知道我可以通过使用@cash引用本地类变量来轻松解决此问题,但是我在OOP上读过的一本书建议几乎不要使用@,而是设置attr并仅使用“ cash”。 I have set attr_accessor, but it doesn't work, and I'd like to understand why. 我设置了attr_accessor,但是它不起作用,我想了解原因。 Thanks 谢谢

class Person
  attr_accessor :cash

  def initialize(cash)
    @cash = cash
  end

  def add_cash(amount)
    cash = cash + amount
  end
end

Local variable reference has precedence over method call with the same name. 局部变量引用优先于具有相同名称的方法调用。

Similarly, local variable assignment has precedence over method call with the same name. 同样,局部变量赋值优先于具有相同名称的方法调用。 A writer method of the form foo= needs an explicit receiver. 格式为foo= writer方法需要一个显式的接收者。 When the receiver is omitted, it is not recognized as a method, but as a local variable assignment. 如果省略接收器,则不会将其识别为方法,而是将其识别为局部变量。

def add_cash(amount)
  self.cash = cash + amount
end

Change add_cash method to: add_cash方法更改为:

  def add_cash(amount)
    @cash = cash + amount  # using cash= will define a local variable in the method,
                           # then cash + amount will become nil + amount
                           # or you could use self.cash instead of @cash
  end

UPDATE 更新

You can do these compares: 您可以进行以下比较:

  def add_cash_local(amount)
    puts "Instance cash: #{cash}"    # we can use cash for the same effect of self.cash or @cash                  
                                     # If there is no local variable assigned as cash
    puts "Instance cash + amount: #{cash + amount}"

    cash = 100
    puts "Local cash: #{cash}"       # after cash= assignment, cash is differ from self.cash or @cash
    puts "Local cash + amount: #{cash + amount}"

    puts "Instance cash: #{@cash}"
    puts "Instance cash + amount: #{@cash + amount}"

    puts "Instance cash: #{self.cash}"
    puts "Instance cash + amount: #{self.cash + amount}"

  end

The error should be (as for why you got `-', I have no idea unless you didn't show all your code and your method calls): 错误应该是(关于为什么得到“-”,除非您没有显示所有代码和方法调用,否则我不知道):

undefined method `+' for nil:NilClass (NoMethodError) nil:NilClass的未定义方法`+'(NoMethodError)

The error in the code stems from the fact that the cash variable is only a local variable. 代码中的错误源于现金变量仅是局部变量的事实。 It is undefined before the call to add_amount, so you see the error to nil:NilClass. 在调用add_amount之前它是未定义的,因此您会看到nil:NilClass的错误。

In the docs for attr_accessor , that is creating set and get methods for an instance (@cash) variable. attr_accessor的文档中,它正在为实例变量(@cash)创建set和get方法。 So, you need to reference the arguments as instance variables when you declare a class. 因此,在声明类时,需要将参数作为实例变量进行引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM