我想写一点“弃用它”库,并使用“method_added”回调很多。 但现在我注意到,当包含一个模块时,这个回调没有被触发。红宝石method_added回调不触发,包括模块
有没有任何回调或变通方法,当somewhing被包含到自身中时,通知类“Foobar”?
小的演示证明:
# Including Moduls won't trigger method_added callback
module InvisibleMethod
def invisible
"You won't get a callback from me"
end
end
class Foobar
def self.method_added(m)
puts "InstanceMethod: '#{m}' added to '#{self}'"
end
def visible
"You will get a callback from me"
end
include InvisibleMethod
end
[:invisible, :visible, :wont_exist].each do |meth|
puts "#{meth}: #{Foobar.public_method_defined? meth}"
end
这就是结果:
InstanceMethod: 'visible' added to 'Foobar'
invisible: true
visible: true
wont_exist: false
附加信息:
我真的需要使用一个钩状method_added。
ActiveModel在运行时通过匿名模块将public_instance_methods添加到类。
不幸的是我不认为有一个很好的答案,但这可能会指向你的一些黑客工作的方向.. http:// stackoverflow。com/questions/4191214/callback-for-classes-defined-within-a-module – David 2012-02-21 23:45:53