下面的「瘋狂性」是否有實際應用?信任剛剛返回的對象?
看起來這是ted
總是能夠自己重返世界,人們會認爲他們在談論到ted
他們希望採取行動以一定的方式和達到一定年齡誰的方式......但他並不是他描繪自己的方式,而是對某人的年齡撒謊。
什麼「掛羊頭賣狗肉」是可能的,當返回一個對象,你檢查什麼對象代表並能......但是真的那麼對象是行使職權的其他方式能夠其它的東西,然後返回。
class Person
def age
21
end
def who_am_i?
puts "I am #{self}/#{object_id} and I am #{age} years old"
self
end
end
ted = Person.new
def ted.singleton_who_am_i?
class << self
def age
0
end
end
puts "I am #{self}/#{object_id} and I am #{age} years old"
self
end
puts ted.who_am_i? == ted.singleton_who_am_i?
>> I am #<Person:0x100138340>/2148123040 and I am 21 years old
>> I am #<Person:0x100138340>/2148123040 and I am 0 years old
>> true