我特别喜欢使用method_missing
,尤其是当您要使用的代码在各种方法调用中非常相似时。这是该站点的一个示例- 每当有人调用x.boo
但boo
不存在时,method_missing 会使用boo
、 的参数boo
和(可选)块调用:
class ActiveRecord::Base
def method_missing(meth, *args, &block)
if meth.to_s =~ /^find_by_(.+)$/
run_find_by_method($1, *args, &block)
else
super # You *must* call super if you don't handle the
# method, otherwise you'll mess up Ruby's method
# lookup.
end
end
def run_find_by_method(attrs, *args, &block)
# Make an array of attribute names
attrs = attrs.split('_and_')
# #transpose will zip the two arrays together like so:
# [[:a, :b, :c], [1, 2, 3]].transpose
# # => [[:a, 1], [:b, 2], [:c, 3]]
attrs_with_args = [attrs, args].transpose
# Hash[] will take the passed associative array and turn it
# into a hash like so:
# Hash[[[:a, 2], [:b, 4]]] # => { :a => 2, :b => 4 }
conditions = Hash[attrs_with_args]
# #where and #all are new AREL goodness that will find all
# records matching our conditions
where(conditions).all
end
end
define_method
看起来它也适合你,但我对它的经验比method_missing
. 这是来自同一链接的示例:
%w(user email food).each do |meth|
define_method(meth) { @data[meth.to_sym] }
end