我學習Ruby單身人士和有這樣的代碼的誤解:Ruby類和對象的單身 - 可以訪問
class MyClass
def self.class_singleton_mymethod
end
end
class_singleton = class << MyClass
self
end
puts class_singleton.methods.grep(/mymethod/) # => []
obj = MyClass.new
def obj.object_singleton_mymethod
end
object_singleton = class << obj
self
end
puts object_singleton.methods.grep(/mymethod/) # => class_singleton_mymethod
爲什麼class_singleton 不包含類的類方法和object_singleton代替對象的單方法包含類的類方法?
感謝您的明確答案! –
@Vladimir:謝謝,我花了兩次嘗試才把它做好,但我很高興能有一個令我滿意的答案。 :-) –