国语精品91自产拍在线观看二区_色偷偷五月天_天天射夜夜爽_99久久免费国产特黄_1717国产精品久久

ruby聲明式語法的實(shí)現(xiàn)代碼

時(shí)間:2022-06-24 14:20:04 聲明范文 我要投稿
  • 相關(guān)推薦

ruby聲明式語法的實(shí)現(xiàn)代碼

  在ActiveRecord可以用很方便的聲明方式來定義model之間的關(guān)聯(lián)關(guān)系,例如:

  class Topic < ActiveRecord::Base

  has_many :posts

  belongs_to :user

  end

  has_many和belongs_to其實(shí)是Topic類的class method,標(biāo)準(zhǔn)寫法是:

  class Topic < ActiveRecord::Base

  Topic.has_many(:posts)

  Topic.belongs_to(:user)

  end

  那么has_many可以給我們帶來什么呢?類方法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)來了。 讓我們來自己試試看吧:

  復(fù)制代碼 代碼如下:

  module M

  def self.included(c)

  c.extend(G)

  end

  module G

  def generate_method(*args)

  args.each do |method_name|

  define_method(method_name) { puts method_name }

  end

  end

  end

  end

  class C

  include M

  generate_method :method1, :method2

  end

  c = C.new

  c.method1

  c.method2

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

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

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

  通過在類C里面聲明generate_method :method1, :method2,讓類C動(dòng)態(tài)的添加了兩個(gè)實(shí)例方法method1,method2,是不是很有意思? 實(shí)際上rails的對(duì)象關(guān)聯(lián)聲明也是以同樣的方式實(shí)現(xiàn)的。

【ruby聲明式語法的實(shí)現(xiàn)代碼】相關(guān)文章:

js鍵盤操作實(shí)現(xiàn)div的移動(dòng)或改變的原理及代碼07-04

廣告編程代碼06-26

員工的聲明10-13

[精選]承諾的聲明11-08

承諾的聲明01-23

道歉的聲明03-19

聲明的公告01-04

公司的聲明05-12

道歉的聲明【精選】08-01

版權(quán)的聲明04-11