我在initialize
中调用了一个私有方法,并且引发了no方法错误。如果我注释掉私有方法,它可以正常工作。我想我使用私有方法有错误的概念,对吧?
in `initialize': private method `start' called for #<RemoteFocusAutomation::Autofocus:0x007fcfed00a3d8> (NoMethodError)
答案 0 :(得分:3)
从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!!