I am in the ruby console, and I am trying to invoke a method and I am not getting the syntax right.
我在ruby控制台,我试图调用一个方法,我没有正确的语法。
Here is the structure of the file:
这是文件的结构:
module App
module Tools
module Pollers
class Kpi
attr_reader :start_time,:stop_time
def initialize(start_time,stop_time)
@start_time = start_time
@stop_time = stop_time
end
.....
and I am trying to invoke this in the console like this:
我试图在控制台中调用这样:
?> kpi = App::Tools::Pollers::Kpi.initialize(start,end_date)
SyntaxError: compile error
(irb):17: syntax error, unexpected tCONSTANT, expecting kDO or '{' or '('
Would someone be able to point me to the right syntax for invoking the initialize ?
有人能够指出我正确的语法来调用初始化吗?
Thanks!
3 个解决方案
#1
3
Judging by the error message, the problem is elsewhere, but you'll probably want Kpi.new
, not Kpi.initialize
.
从错误消息判断,问题出在其他地方,但你可能想要Kpi.new,而不是Kpi.initialize。
Well, apparently it's not elsewhere :)
好吧,显然它不在其他地方:)
#2
7
initialize
is an instance method of class Kpi
. App::Tools::Pollers::Kpi
is a class, and can only have class methods ran on it. Ruby provides a class method for initialization to every class for free. This method is new
. Call new
, which takes the same arguments as your initialize
, and returns an instance:
initialize是类Kpi的实例方法。 App :: Tools :: Pollers :: Kpi是一个类,只能运行类方法。 Ruby为每个类提供了一个免费初始化的类方法。这种方法是新的。调用new,它接受与initialize相同的参数,并返回一个实例:
kpi = App::Tools::Pollers::Kpi.new(start,end_date)
#3
3
It would be
这将是
App::Tools::Pollers::Kpi.new(start,end_date)
There also seems to be a syntax error earlier in your irb session.
您的irb会话中似乎还存在语法错误。
#1
3
Judging by the error message, the problem is elsewhere, but you'll probably want Kpi.new
, not Kpi.initialize
.
从错误消息判断,问题出在其他地方,但你可能想要Kpi.new,而不是Kpi.initialize。
Well, apparently it's not elsewhere :)
好吧,显然它不在其他地方:)
#2
7
initialize
is an instance method of class Kpi
. App::Tools::Pollers::Kpi
is a class, and can only have class methods ran on it. Ruby provides a class method for initialization to every class for free. This method is new
. Call new
, which takes the same arguments as your initialize
, and returns an instance:
initialize是类Kpi的实例方法。 App :: Tools :: Pollers :: Kpi是一个类,只能运行类方法。 Ruby为每个类提供了一个免费初始化的类方法。这种方法是新的。调用new,它接受与initialize相同的参数,并返回一个实例:
kpi = App::Tools::Pollers::Kpi.new(start,end_date)
#3
3
It would be
这将是
App::Tools::Pollers::Kpi.new(start,end_date)
There also seems to be a syntax error earlier in your irb session.
您的irb会话中似乎还存在语法错误。