偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Ruby DSL特點(diǎn)分析介紹

開發(fā) 開發(fā)工具
我們希望大家通過(guò)對(duì)本文中介紹的Ruby DSL特點(diǎn)介紹,可以幫助大家提高對(duì)Ruby語(yǔ)言的了解程度,積累我們的編程經(jīng)驗(yàn)。

Ruby語(yǔ)言是一個(gè)應(yīng)用靈活的解釋型腳本語(yǔ)言。對(duì)于一個(gè)編程人員來(lái)說(shuō),Ruby DSL是一個(gè)功能強(qiáng)大的工具。下面我們就來(lái)一起看看Ruby DSL特點(diǎn)介紹。#t#

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

  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究竟是什么東西呢?其實(shí)他們是Topic類的class method,Ruby DSL特點(diǎn)的標(biāo)準(zhǔn)寫法是: 

  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可以給我們帶來(lái)什么呢?類方法has_many在被執(zhí)行的時(shí)候,給Topic的對(duì)象實(shí)例添加了一系列方法:posts, posts<<, orders.push......等等。所以當(dāng)我們?cè)趍odel里面聲明has_many,belongs_to等對(duì)象關(guān)系的時(shí)候,一系列相關(guān)的對(duì)象方法就被自動(dòng)添加進(jìn)來(lái)了。

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

 

  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 

 

 

我們定義了一個(gè)聲明generate_method,可以接受多個(gè)symbol,來(lái)動(dòng)態(tài)的創(chuàng)建同名的方法。現(xiàn)在我們?cè)陬怌里面使用這個(gè)聲明:generate_method :method1, :method2,當(dāng)然我們需要include模塊M。為什么rails的model不需要include相關(guān)的模塊呢?當(dāng)然是因?yàn)門opic的父類ActiveRecord::Base已經(jīng)include了模塊Associations了。

類C通過(guò)include模塊M,調(diào)用了模塊M的一個(gè)included回調(diào)接口,讓類C去extend模塊G,換句話來(lái)說(shuō)就是,通過(guò)include模塊M,來(lái)給類C動(dòng)態(tài)添加一個(gè)類方法generate_method。

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

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

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

以上就是對(duì)Ruby DSL特點(diǎn)的分析介紹。

責(zé)任編輯:曹凱 來(lái)源: 百度博客
相關(guān)推薦

2009-12-14 13:56:12

Ruby特點(diǎn)

2009-12-17 10:29:04

Ruby異常處理結(jié)構(gòu)

2009-12-14 18:23:38

Ruby DSL測(cè)試

2009-12-14 18:14:27

Ruby DSL

2009-12-14 15:04:32

Ruby性能特點(diǎn)

2010-01-27 16:41:48

Android特點(diǎn)

2009-12-14 13:06:08

Ruby數(shù)字類型

2009-12-29 13:29:28

WPF Depende

2010-03-10 18:51:18

Python語(yǔ)言

2009-12-24 10:09:33

WPF事件注冊(cè)

2010-02-23 09:51:32

WCF MTOM

2009-12-17 17:13:23

Ruby for Ec

2009-12-14 16:26:40

Ruby復(fù)制文件

2009-12-15 15:19:30

Ruby訪問(wèn)控制

2009-12-14 13:27:06

Ruby區(qū)間

2009-12-15 11:31:53

Ruby self

2009-12-15 18:39:36

Ruby Active

2009-12-18 14:59:54

Ruby標(biāo)識(shí)名

2009-12-25 16:05:24

WPF 4.0特點(diǎn)

2009-12-29 16:21:46

silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)