自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談Ruby on Rails中的include和extend

開發(fā) 開發(fā)工具
本文將介紹淺談Ruby on Rails中的include和extend。include主要用來將一個模塊插入(mix)到一個類或者其它模塊。extend 用來在一個對象(object,或者說是instance)中引入一個模塊,這個類從而也具備了這個模塊的方法。

從模塊引入方法、變量,使得編程變得簡單,擴展性愈強,比以往的類的繼承更靈活。這樣的引入,仿佛將一個方法塊,復制了一份放到了你所引用的類或者模塊里面。你完全可以將多個互不相干的類中相同的方法拿出來寫到一個模塊中,這樣可以使得代碼精簡,符合Ruby的設計初衷,而且,使得你的程序更有條理。

Ruby on Rails常見用法

通常引用模塊有以下3種情況:

1.在類定義中引入模塊,使模塊中的方法成為類的實例方法

這種情況是最常見的

直接 include 即可

2.在類定義中引入模塊,使模塊中的方法成為類的類方法

這種情況也是比較常見的

直接 extend 即可

3.在類定義中引入模塊,既希望引入實例方法,也希望引入類方法

這個時候需要使用 include,

但是在模塊中對類方法的定義有不同,定義出現在self.included塊中

def self.included(c) ... end 中

Ruby on Rails實例講解

Code

  1. #基類  
  2. module Base  
  3.   #顯示  
  4.   def show  
  5.     puts "You came here!"  
  6.   end  
  7. end  
  8. class Car  
  9.   extend Base   #擴展了類方法,我們可以通過Car.show調用  
  10. end  
  11. class Bus  
  12.   include Base  #擴展了實例方法,可以通過Bus.new.show調用  
  13. end 

但是我們經常有這樣的需要,希望基類足夠強大,既可以擴展實例方法,也可以擴展類方法,Ruby on Rails同樣提供了解決方案。

  1. Code  
  2. #基類  
  3. module Base  
  4.   def show  
  5.     puts "You came here!"  
  6.   end  
  7.  
  8.   #擴展類方法  
  9.   def self.included(base)  
  10.     def base.call  
  11.       puts "I'm strong!"  
  12.     end  
  13.     base.extend(ClassMethods)  
  14.   end  
  15.  
  16.   #類方法  
  17.   module ClassMethods  
  18.     def hello  
  19.       puts "Hello baby!"  
  20.     end  
  21.   end  
  22.      
  23. end  
  24.  
  25. class Bus  
  26.   include Base  
  27. end 

此時Bus已經具備了實例方法show,類方法:call 、hello,訪問方式

  1. Bus.new.show  
  2. Bus.call  
  3. Bus.hello 

肯定也有人提出此類疑問,使用extend能夠實現此功能不?

答案是:暫未找到,如您找到請明示,多謝!

我也曾經做過以下實驗,結果沒有成功,在此也張貼出來,希望能給您帶來一些啟示。

  1. Code  
  2. #基類  
  3. module Base  
  4.   def show  
  5.     puts "You came here!"  
  6.   end  
  7.     
  8.     #擴展實例方法  
  9.   def self.extended(base)  
  10.     base.extend(InstanceMethods)  
  11.   end  
  12.  
  13.   module InstanceMethods  
  14.     def love  
  15.       puts 'We are instances,loving each other!'  
  16.     end  
  17.   end  
  18.  
  19. end  
  20.  
  21. class Car  
  22.   extend Base  
  23. end 

但是這樣,實例方法擴展失敗,依然擴展了類方法

  1. Car.show  
  2. Car.love             #類方法  
  3. Car.new.love      #undefined method 'love'  

【編輯推薦】

  1. Ruby語言的發(fā)展趨勢和啟示
  2. Ruby on Rails為企業(yè)SOA做好準備了嗎
  3. 腳本語言排行榜 PHP、Ruby和Python領先
  4. 讓Ruby性能增加30%的改進方法分析
  5. 對Ruby VS Java誤區(qū)的深度剖析
責任編輯:彭凡 來源: cnblogs
相關推薦

2009-08-27 10:21:22

Ruby on Rai

2009-07-17 17:49:39

JRuby學習

2009-12-17 15:29:00

Rails方法exte

2009-08-06 09:13:36

Ruby on Rai

2009-09-29 17:04:29

2010-09-25 14:39:29

Bruce Tate

2012-06-11 09:44:10

微軟AzurePython

2015-10-10 11:00:05

RubyRails性能

2009-12-16 16:37:59

Ruby on Rai

2015-10-14 17:27:18

性能

2009-12-17 14:29:50

Ruby on Rai

2009-12-14 15:30:43

安裝Ruby on R

2009-07-20 09:12:54

Ruby on Rai

2009-06-17 10:08:32

Ruby on Rai安裝Ruby

2010-10-09 08:58:03

NginxRuby on Rai

2009-12-16 16:24:00

Ruby on Rai

2010-07-12 09:22:05

RubyRuby on rai

2013-03-28 12:42:02

RubyRails

2009-12-16 15:23:33

Ruby on rai

2009-12-16 15:41:10

Ruby on Rai
點贊
收藏

51CTO技術棧公眾號