我可以創建可以通過類方法調用的私有實例方法嗎?從Ruby的類方法調用私有實例方法
class Foo
def initialize(n)
@n = n
end
private # or protected?
def plus(n)
@n += n
end
end
class Foo
def Foo.bar(my_instance, n)
my_instance.plus(n)
end
end
a = Foo.new(5)
a.plus(3) # This should not be allowed, but
Foo.bar(a, 3) # I want to allow this
道歉,如果這是一個非常基本的問題,但我還沒有能夠谷歌我的方式解決方案。
您應該解決你的問題,你有一個錯字。這些方法叫做bar或plus嗎? – Samuel 2009-01-07 15:54:52