尝试在Ruby中调用方法时出现异常
发布时间:2020-12-17 03:04:13 所属栏目:百科 来源:网络整理
导读:我是 Ruby的新手.我的示例代码给了我这个例外: C:/Users/abc/RubymineProjects/Sample/hello.rb:5:in `class:Hello': undefined method `first_method' for Hello:Class (NoMethodError) from C:/Users/abc/RubymineProjects/Sample/hello.rb:1:in `top (re
我是
Ruby的新手.我的示例代码给了我这个例外:
C:/Users/abc/RubymineProjects/Sample/hello.rb:5:in `<class:Hello>': undefined method `first_method' for Hello:Class (NoMethodError) from C:/Users/abc/RubymineProjects/Sample/hello.rb:1:in `<top (required)>' from -e:1:in `load' from -e:1:in `<main>' 进程以退出代码1结束 我的代码是: class Hello def first_method puts "Hello World" end first_method() end 我正在使用RubyMine 4.5.4. 解决方法
问题是你试图在类上调用first_method – 而first_method是一个实例方法.要调用实例方法,您需要使用该类的实例.要创建类的实例,可以使用SomeClass.new.因此,要使用您的方法,请尝试此代码(与@megas相同的代码):
class Hello def first_method puts "Hello World" end end Hello.new.first_method (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |