我在 中调用了一个私有方法initialize
,并引发了一个无方法错误。如果我注释掉私有方法,它工作正常。我想我对使用私有方法有错误的概念,对吧?
in `initialize': private method `start' called for #<RemoteFocusAutomation::Autofocus:0x007fcfed00a3d8> (NoMethodError)
我在 中调用了一个私有方法initialize
,并引发了一个无方法错误。如果我注释掉私有方法,它工作正常。我想我对使用私有方法有错误的概念,对吧?
in `initialize': private method `start' called for #<RemoteFocusAutomation::Autofocus:0x007fcfed00a3d8> (NoMethodError)
self
从self.start(args)
您的Autofocus#initialize
方法定义中删除。您不应该在 ruby 中调用具有显式接收器的私有方法。它必须是隐式调用。
这是一个例子:
# I tried to call the private method with explicit receiver,which I supposed no to do,
# as Ruby wouldn't allow me,and will give me back error.
class Foo
def initialize
self.foo
end
private
def foo;1;end
end
Foo.new
# `initialize': private method `foo' called for # (NoMethodError)
现在我正在做 Ruby 允许我做的事情:
class Foo
def initialize
foo
end
private
def foo;p 1;end
end
Foo.new # => 1 # works!!