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

Ruby DSL特點分析介紹

開發(fā) 開發(fā)工具
我們希望大家通過對本文中介紹的Ruby DSL特點介紹,可以幫助大家提高對Ruby語言的了解程度,積累我們的編程經驗。

Ruby語言是一個應用靈活的解釋型腳本語言。對于一個編程人員來說,Ruby DSL是一個功能強大的工具。下面我們就來一起看看Ruby DSL特點介紹。#t#

在rails里面,我們可以用很方便的聲明方式來定義model之間的關聯(lián)關系,Ruby DSL特點例如:

  1. class Topic < Active
    Record::Base
       
  2. has_many :posts   
  3. belongs_to :user   
  4. end   
  5. class Topic < Active
    Record::Base
     
  6. has_many :posts  
  7. belongs_to :user  
  8. end 

 


那has_many和belongs_to究竟是什么東西呢?其實他們是Topic類的class method,Ruby DSL特點的標準寫法是: 

  1. class Topic < ActiveRecord::Base   
  2. Topic.has_many(:posts)   
  3. Topic.belongs_to(:user)   
  4. end   
  5. class Topic < ActiveRecord::Base 
  6. Topic.has_many(:posts)  
  7. Topic.belongs_to(:user)  
  8. end 

 

那么has_many可以給我們帶來什么呢?類方法has_many在被執(zhí)行的時候,給Topic的對象實例添加了一系列方法:posts, posts<<, orders.push......等等。所以當我們在model里面聲明has_many,belongs_to等對象關系的時候,一系列相關的對象方法就被自動添加進來了。

既然明白了rails的小把戲,讓我們來自己試試看吧:

 

  1. module M   
  2. def self.included(c)   
  3. c.extend(G)   
  4. end   
  5. module G   
  6. def generate_method(*args)   
  7. args.each do |method_name|   
  8. define_method(method_name) 
    { puts method_name }   
  9. end   
  10. end   
  11. end   
  12. end   
  13. class C   
  14. include M   
  15. generate_method :method1, :method2   
  16. end   
  17. c = C.new   
  18. c.method1   
  19. c.method2   
  20. module M  
  21. def self.included(c)  
  22. c.extend(G)  
  23. end  
  24. module G  
  25. def generate_method(*args)  
  26. args.each do |method_name|  
  27. define_method(method_name) 
    { puts method_name }  
  28. end  
  29. end  
  30. end  
  31. end  
  32. class C  
  33. include M  
  34. generate_method :method1, :method2  
  35. end  
  36. c = C.new  
  37. c.method1  
  38. c.method2 

 

 

我們定義了一個聲明generate_method,可以接受多個symbol,來動態(tài)的創(chuàng)建同名的方法?,F(xiàn)在我們在類C里面使用這個聲明:generate_method :method1, :method2,當然我們需要include模塊M。為什么rails的model不需要include相關的模塊呢?當然是因為Topic的父類ActiveRecord::Base已經include了模塊Associations了。

類C通過include模塊M,調用了模塊M的一個included回調接口,讓類C去extend模塊G,換句話來說就是,通過include模塊M,來給類C動態(tài)添加一個類方法generate_method。

這個generate_method被定義在模塊G當中,它接受一系列參數(shù),來動態(tài)創(chuàng)建相關的方法。于是我們就實現(xiàn)了這樣的DSL功能:

通過在類C里面聲明generate_method :method1, :method2,讓類C動態(tài)的添加了兩個實例方法method1,method2,是不是很有意思?

實際上rails的對象關聯(lián)聲明也是以同樣的方式實現(xiàn)的。

以上就是對Ruby DSL特點的分析介紹。

責任編輯:曹凱 來源: 百度博客
相關推薦

2009-12-14 13:56:12

Ruby特點

2009-12-17 10:29:04

Ruby異常處理結構

2009-12-14 18:14:27

Ruby DSL

2009-12-14 18:23:38

Ruby DSL測試

2009-12-14 15:04:32

Ruby性能特點

2010-01-27 16:41:48

Android特點

2009-12-14 13:06:08

Ruby數(shù)字類型

2009-12-24 10:09:33

WPF事件注冊

2010-02-23 09:51:32

WCF MTOM

2009-12-15 11:31:53

Ruby self

2009-12-18 14:59:54

Ruby標識名

2009-12-14 13:27:06

Ruby區(qū)間

2009-12-15 18:39:36

Ruby Active

2010-03-10 18:51:18

Python語言

2009-12-29 13:29:28

WPF Depende

2009-12-17 17:13:23

Ruby for Ec

2009-12-15 15:19:30

Ruby訪問控制

2009-12-14 16:26:40

Ruby復制文件

2009-12-29 16:21:46

silverlight

2009-12-25 16:05:24

WPF 4.0特點
點贊
收藏

51CTO技術棧公眾號