ruby元编程之创建自己的动态方法
method_missing是Ruby元编程(metaprogramming)常用的手法。基本思想是通过实现调用不存在的方法,以便进行回调。典型的例子是:ActiveRecord的动态查找(dynamicfinder)。例如:我们有email属性那么就可以调用User.find_by_email('joe@example.com'),虽然,ActiveRecord::Base并没有一个叫做find_by_email的方法。
respond_to?并不如method_missing出名,常用在当需要确认一个回馈对象需要确认,以便不会因为没有反馈对象,而导致后面的调用出现错误。
下面是一个应用这两者的例子:
示例
我们有类Legislatorclass,现在,想要给它加一个find_by_first_name('John')的动态调用。实现find(:first_name=>'John')的功能。
classLegislator
#假设这是一个真实的实现
deffind(conditions={})
end
#在本身定义毕竟这是他的方法
defself.method_missing(method_sym,*arguments,&block)
#thefirstargumentisaSymbol,soyouneedto_sitifyouwanttopatternmatch
ifmethod_sym.to_s=~/^find_by_(.*)$/
find($1.to_sym=>arguments.first)
else
super
end
end
end
那么这个时候调用
Legislator.respond_to?(:find_by_first_name)
将会提示错误,那么继续
classLegislator
#省略
#It'simportanttoknowObjectdefinesrespond_tototaketwoparameters:themethodtocheck,andwhethertoincludeprivatemethods
#http://www.ruby-doc.org/core/classes/Object.html#M000333
defself.respond_to?(method_sym,include_private=false)
ifmethod_sym.to_s=~/^find_by_(.*)$/
true
else
super
end
end
end
正如代码注释所述respond_to?需要两个参数,如果,你没有提供将会产生ArgumentError。
相关反射DRY
如果我们注意到了这里有重复的代码。我们可以参考ActiveRecord的实现封装在ActiveRecord::DynamicFinderMatch,以便避免在method_missing和respond_to?中重复。
classLegislatorDynamicFinderMatch
attr_accessor:attribute
definitialize(method_sym)
ifmethod_sym.to_s=~/^find_by_(.*)$/
@attribute=$1.to_sym
end
end
defmatch?
@attribute!=nil
end
end
classLegislator defself.method_missing(method_sym,*arguments,&block) match=LegislatorDynamicFinderMatch.new(method_sym) ifmatch.match? find(match.attribute=>arguments.first) else super end end
defself.respond_to?(method_sym,include_private=false) ifLegislatorDynamicFinderMatch.new(method_sym).match? true else super end end end