The append_features method of Module

In the same way that the class could include some singleton methods,the Module could include some singleton methods,too.But if we implement a class which include a Module which contians some singleton methods,does these methods become to the class's singleton methods?The answer is 'no'.There is a hook called append_features that we can override.It's called with a parameter that is the destination class or module.For an example of its use,see the following codes:
ruby 代碼
 
  1. module MyMod  
  2.   def MyMod.append_features(someClass)  
  3.     def someClass.modmeth  
  4.       puts "Module (class) method"  
  5.     end  
  6.     super  
  7.   end  
  8. end  
  9. class AppendFeatures  
  10.   include MyMod  
  11.   def initialize  
  12.       
  13.   end  
  14. end  
  15.   
  16. AppendFeatures.modmeth  

Note:
you should call the "super" when you override the append_features method.

It's very interesting.
These code copy from the <ruby way=""> book.</ruby>
發佈了26 篇原創文章 · 獲贊 0 · 訪問量 1851
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章