Ruby:NoMethodError,但为什么呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26358039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:36:11  来源:igfitidea点击:

Ruby: NoMethodError, but why?

rubynomethoderror

提问by Annie the Eagle

I was working on a simple Pi Generator while learning Ruby, but I kept getting NoMethodError on RubyMine 6.3.3, so I decided to make a new project and new class with as simple as possible, and I STILLget NoMethodError. Any reason?

我在学习 Ruby 时正在开发一个简单的 Pi 生成器,但是我在 RubyMine 6.3.3 上不断收到 NoMethodError,所以我决定尽可能简单地创建一个新项目和新类,但我仍然收到 NoMethodError。任何原因?

class Methods
  def hello (player)
    print "Hello, " << player
  end
  hello ("Annie")
end

And the error I get is:

我得到的错误是:

C:/Users/Annie the Eagle/Documents/Coding/Ruby/Learning Environment/methods.rb:5:in `<class:Methods>': undefined method `hello' for Methods:Class (NoMethodError)

回答by Arup Rakshit

You have defined an instance methodand are trying to call it as a method of a class. Thus you need to make the method helloa class method, not an instance method of the class Methods.

您已经定义了一个实例方法并试图将它作为方法来调用。因此,您需要使该方法hello成为类方法,而不是类的实例方法Methods

class Methods
  def self.hello(player)
    print "Hello, " << player
  end
  hello("Annie")
end

Or, if you want to define it as instance methodthen call it as below :

或者,如果要将其定义为实例方法,请按如下方式调用它:

class Methods
  def hello(player)
    print "Hello, " << player
  end
end
Methods.new.hello("Annie")

回答by joshua.paling

You're trying to call an instance method as a class method.

您正在尝试将实例方法作为类方法调用。

Here's some code that illustrates the difference between the two in ruby:

下面是一些代码,说明了两者在 ruby​​ 中的区别:

class Person

  # This is a class method - note it's prefixed by self
  # (which in this context refers to the Person class)
  def self.species
    puts 'Human'
    # Note: species is OK as a class method because it's the same 
    # for all instances of the person class - ie, 'Bob', 'Mary', 
    # 'Peggy-Sue', and whoever else, are ALL Human.
  end

  # The methods below aren't prefixed with self., and are
  # therefore instance methods

  # This is the construct, called automatically when
  # a new object is created
  def initialize(name)
    # @name is an instance variable
    @name = name
  end

  def say_hello
    puts "Hello from #{@name}"
  end

end

And now try it out, calling the methods...

现在尝试一下,调用方法......

# Call a class method...
# We're not referring to any one 'instance' of Person,
Person.species #=> 'Human'

# Create an instance
bob = Person.new('Bob')

# Call a method on the 'Bob' instance
bob.say_hello #=> 'Hello from Bob'

# Call a method on the Person class, going through the bob instance
bob.class.species #=> 'Human'

# Try to call the class method directly on the instance
bob.species #=> NoMethodError

# Try to call the instance method on the class
# (this is the error you are getting)
Person.say_hello #=> NoMethodError

回答by Sara Tibbetts

You've created an instance method, but you're calling a class method. In order to call hello("Annie"), you have to make an instance of Methods. For instance:

您已经创建了一个实例方法,但是您正在调用一个类方法。为了调用hello("Annie"),您必须创建一个方法实例。例如:

class Methods
  def self.hello(player)
      print "Hello, " << player
  end
end

my_method = Methods.new
my_method.hello("Annie")

This would output Hello, Annie

这将输出 Hello, Annie

回答by sz23

By defining a method with def method_name argsyou are defining a instance method that will be included in every object of that class, but not in the class itself.

通过使用def method_name args定义一个方法,您正在定义一个实例方法,该方法将包含在该类的每个对象中,但不包含在类本身中。

On the other hand, by def self.method_name argsyou will get a class method that will be directly in the class, without the need of instanciate an object from it.

另一方面,通过def self.method_name args,您将获得一个直接在类中的类方法,而无需从中实例化一个对象。

So If you have this:

所以如果你有这个:

Class Test
   def self.bar
   end

   def foo
   end

end

You can execute the instance method this way:

您可以通过以下方式执行实例方法:

a = Test.new
a.foo

And as for the class one should be:

至于第一类应该是:

Test.foo