ruby宣告式語法的實現程式碼

來源:果殼範文吧 9.08K

在ActiveRecord可以用很方便的宣告方式來定義model之間的關聯關係,例如:

ruby宣告式語法的實現程式碼

class Topic < ActiveRecord::Base

has_many :posts

belongs_to :user

end

has_many和belongs_to其實是Topic類的class method,標準寫法是:

class Topic < ActiveRecord::Base

_many(:posts)

ngs_to(:user)

end

那麼has_many可以給我們帶來什麼呢?類方法has_many在被執行的時候,給Topic的物件例項添加了一系列方法:posts, posts<<, ......等等。所以當我們在model裡面宣告has_many,belongs_to等物件關係的時候,一系列相關的物件方法就被自動新增進來了。 讓我們來自己試試看吧:

複製程式碼 程式碼如下:

module M

def uded(c)

nd(G)

end

module G

def generate_method(*args)

do |method_name|

define_method(method_name) { puts method_name }

end

end

end

end

class C

include M

generate_method :method1, :method2

end

c =

od1

od2

我們定義了一個宣告generate_method,可以接受多個symbol,來動態的建立同名的方法。現在我們在類C裡面使用這個宣告:generate_method :method1, :method2,當然我們需要include模組M。為什麼ActiveRecord的model不需要include相關的模組呢?當然是因為Topic的父類ActiveRecord::Base已經include了模組Associations了。

類C通過include模組M,呼叫了模組M的一個included回撥介面,讓類C去extend模組G,換句話來說就是,通過include模組M,來給類C動態新增一個類方法generate_method。

這個generate_method被定義在模組G當中,它接受一系列引數,來動態建立相關的方法。於是我們就實現了這樣的`DSL功能:

通過在類C裡面宣告generate_method :method1, :method2,讓類C動態的添加了兩個例項方法method1,method2,是不是很有意思? 實際上rails的物件關聯宣告也是以同樣的方式實現的。

熱門標籤