Ruby 作为一种面向对象的语言。这意味着无论我发送什么消息,我都严格将其发送到类的某个对象/实例上。
例子:
class Test
def test1
puts "I am in test1. A public method"
self.test2
end
def test2
puts "I am in test2. A public Method"
end
end
有意义我test2在self对象上调用方法
但我不能这样做
class Test
def test1
puts "I am in test1. A public method"
self.test2 # Don't work
test2 # works. (where is the object that I am calling this method on?)
end
private
def test2
puts "I am in test2. A private Method"
end
end
我什么时候test2可以public method调用它self(很公平,一个发送给 self 对象的方法)。但是什么时候test2我private method不能自己打电话。那么我发送方法的对象在哪里?