method_missing是Ruby元編程(metaprogramming)常用的手法。基本思想是通過(guò)實(shí)現(xiàn)調(diào)用不存在的方法,以便進(jìn)行回調(diào)。典型的例子是:ActiveRecord的動(dòng)態(tài)查找(dynamic finder)。例如:我們有email屬性那么就可以調(diào)用User.find_by_email('joe@example.com'),雖然, ActiveRecord::Base并沒(méi)有一個(gè)叫做find_by_email的方法。
respond_to? 并不如method_missing出名,常用在當(dāng)需要確認(rèn)一個(gè)回饋對(duì)象需要確認(rèn),以便不會(huì)因?yàn)闆](méi)有反饋對(duì)象,而導(dǎo)致后面的調(diào)用出現(xiàn)錯(cuò)誤。
下面是一個(gè)應(yīng)用這兩者的例子:
示例
我們有類Legislator class,現(xiàn)在,想要給它加一個(gè)find_by_first_name('John')的動(dòng)態(tài)調(diào)用。實(shí)現(xiàn)find(:first_name => 'John')的功能。
代碼如下:
class Legislator
#假設(shè)這是一個(gè)真實(shí)的實(shí)現(xiàn)
def find(conditions = {})
end
#在本身定義畢竟這是他的方法
def self.method_missing(method_sym, *arguments, &block)
# the first argument is a Symbol, so you need to_s it if you want to pattern match
if method_sym.to_s =~ /^find_by_(.*)$/
find($1.to_sym => arguments.first)
else
super
end
end
end
那么這個(gè)時(shí)候調(diào)用
代碼如下:
Legislator.respond_to?(:find_by_first_name)
將會(huì)提示錯(cuò)誤,那么繼續(xù)
代碼如下:
class Legislator
# 省略
# It's important to know Object defines respond_to to take two parameters: the method to check, and whether to include private methods
# http://www.ruby-doc.org/core/classes/Object.html#M000333
def self.respond_to?(method_sym, include_private = false)
if method_sym.to_s =~ /^find_by_(.*)$/
true
else
super
end
end
end
正如代碼注釋所述respond_to?需要兩個(gè)參數(shù),如果,你沒(méi)有提供將會(huì)產(chǎn)生ArgumentError。
相關(guān)反射 DRY
如果我們注意到了這里有重復(fù)的代碼。我們可以參考ActiveRecord的實(shí)現(xiàn)封裝在ActiveRecord::DynamicFinderMatch,以便避免在method_missing和respond_to?中重復(fù)。
代碼如下:
class LegislatorDynamicFinderMatch
attr_accessor :attribute
def initialize(method_sym)
if method_sym.to_s =~ /^find_by_(.*)$/
@attribute = $1.to_sym
end
end
def match?
@attribute != nil
end
end
class Legislator
def self.method_missing(method_sym, *arguments, &block)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注