(1)當調用一個對象的方法時,其實是給一個對象發送了一條消息,ruby中Object#send()方法是,對方法調用本質的詮釋,例:ruby
obj.my_method(3) #與 obj.send(:my_method, 3)等價spa
使用send()方法調用方法,方法名能夠成爲一個參數,便可以動態的調用方法,這種技術叫作動態派發code
(2)能夠利用Module#define_method()方法定義一個方法,結合Object#send()既能夠動態的定義方法,大量減小重複代碼對象
(1)method_missing()是一個kernel方法,當解釋器找不到對象調用的方法時,就會調用method_missing()方法,因此能夠經過重寫method_missing()方法來使用幽靈方法繼承
(2)幽靈方法存在一些須要注意的地方:rem
幽靈方法並非實際存在的方法,全部 respond_to?()方法會返回false,因此在使用幽靈方法時最好也重寫respond_to?()方法,同時,methods()等一些列出方法名的方法返回結果中並不會有幽靈方法it
幽靈方法相對於正常的方法響應時間更長,因爲在方法查找時會將整個祖先鏈走完,才能在最後調用method_missing()方法class
幽靈方法可能會和一個真實的方法名衝突,這時是調用不到幽靈方法的,能夠Module#undef_method()方法,他會刪除全部的(包括繼承來的)方法,或者使用Module#remove_method()方法,它只會刪除接受者本身的方法。最好的方式仍是使用白板類循環
當重寫method_missing()的代碼中出現調用沒有定義的方法,會形成死循環。引用
(3)幽靈方法示例:
class MyOpenStruct def initialize @attributes = {} end def method_messing(name, *args) attributes = name.to_s if attributes =~ /=$/ @attributes[attribute.chop] = args[0] else @attributes[attribute] end end end icecream = MyOpenStruct.new icecream.flavor = "vanilla" icecream.flavor # => "vanilla"
(4)除了Object#method_missing()方法,還有Module#const_missing()方法,當引用一個不存在的常量時,會被調用
(5)方法查找時,先找到接收者的類,再沿着父類向上查找(忽略eigenclass的狀況下),因爲圖已經比較亂了,Class類的class箭頭沒有給出,Class.class #=> Class