class Foo
@@first_time = true
def self.private_bar
if @@first_time
puts "Hi"
else
puts "Oi, you don't work here"
end
@@first_time = false
end
private_class_method :private_bar
public
def calling_private_method
self.class.send :private_bar
another_private_bar
end
end
f=Foo.new
f.calling_private_method
f.class.send :private_bar
輸出應該是這樣的:這是一個違反紅寶石封裝的簡單方法嗎?
Hi
NoMethodError: private method `private_bar'
但是,輸出是:
Hi
Oi, you don't work
這究竟是爲什麼?這必須是一個錯誤,否則它是對信息封裝的重要違反,對嗎?
http://stackoverflow.com/a/896633/10396 – AShelly