【问题标题】:Ruby: NoMethodError, but why?Ruby:NoMethodError,但为什么呢?
【发布时间】:2014-10-14 10:04:51
【问题描述】:

我在学习 Ruby 时正在开发一个简单的 Pi 生成器,但我在 RubyMine 6.3.3 上一直收到 NoMethodError,所以我决定创建一个尽可能简单的新项目和新类,我 STILL 获取 NoMethodError。有什么原因吗?

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

我得到的错误是:

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

【问题讨论】:

  • 您正在调用类定义中的方法。我不确定它的意图。
  • 我不确定您在 OO 编程方面的背景/经验水平,所以请检查一下:您了解类和实例(以及类方法和实例方法)之间的区别吗?答案假设您这样做...

标签: ruby nomethoderror


【解决方案1】:

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

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

或者,如果你想将它定义为实例方法,那么调用它如下:

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

【讨论】:

  • 哦哇...所以 self.hello 与“静态(返回类型)你好”的 C# 等价物相同吗?什么时候可以只使用“def hello”
  • 您应该删除方法名称和括号之间的空格。
【解决方案2】:

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

这里有一些代码说明了两者在 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

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

# 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

【讨论】:

    【解决方案3】:

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

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

    这将输出Hello, Annie

    【讨论】:

      【解决方案4】:

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

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

      如果你有这个:

      Class Test
         def self.bar
         end
      
         def foo
         end
      
      end
      

      你可以这样执行实例方法:

      a = Test.new
      a.foo
      

      至于第一类应该是:

      Test.foo
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-25
        • 2016-06-29
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多