这是一个我似乎无法解释的有趣案例。它看起来像私人定员是'私人的',但有时也有例外。定期私有方法似乎表现不同私人setter方法:为什么私人制定者的行为与其他私人方法有所不同?
class TestClass
def do
self.foo = :bar # fine
self.baz # error
end
private
def foo=(other)
@foo = other
end
def baz
end
end
TestClass.new.do
上述代码设置@foo
就好了,尽管被称为上明确self
。然后它不能拨打#baz
,因为#baz
是一种私人方法。
这是怎么回事?
通常我会叫'baz'不使用前缀'self',以及应在这种情况下工作。但我意识到这并不能回答你的直接问题。 – moveson