我在学习 Ruby 时正在开发一个简单的 Pi 生成器,但我在 RubyMine 6.3.3 上不断收到 NoMethodError,所以我决定用尽可能简单的方式创建一个新项目和新类,并且我STILL得到无方法错误。任何原因?
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)
您已经定义了一个实例方法并试图将其称为一个类的方法。因此你需要制定该方法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")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)