2
看起來像const_missing
是Object
的實例方法。如果是這樣,爲什麼這個代碼不工作?爲什麼const_missing沒有在Object前面加上前綴?
module Extensions
def const_missing(c)
puts c
end
end
class Object
include Extensions
end
NonExistent.new
爲了讓它正常工作,我必須要改變def const_missing
到def Object.const_missing
。爲什麼?
因爲在包含的模塊之前調用了正確的方法。 – 2011-05-21 21:43:16
你是什麼意思「正確的方法」? – ryeguy 2011-05-21 21:44:24
[重寫方法由另一個在模塊中定義]的可能重複(http://stackoverflow.com/questions/5944278/overriding-method-by-another-defined-in-module) – Phrogz 2011-05-21 21:57:42