用于定义方法的 Ruby 钩子? [英] Ruby hook for method defined?
问题描述
我一直在谷歌上搜索这个问题,但没有找到答案,这让我觉得答案是否定的,但我想我会在这里问,以防万一有人确切知道.
I've been googling around for this and haven't been able to find an answer, which makes me think the answer is no, but I figured I'd ask here in case anyone knows for sure.
Ruby 是否有一个钩子来定义方法(即在模块或类上)?
Does Ruby have a hook for when methods are defined (ie on a module or class)?
如果没有,是否有人对 main
对象的实现足够熟悉,知道在顶级定义时它是如何将方法复制到 Object
的?
If not, is anyone familiar enough with the implementation of the main
object to know how exactly it copies methods to Object
when they're defined at the top level?
对这个真的很好奇.感谢您提供任何信息:)
Really curious about this. Thanks for any info :)
推荐答案
确实如此.Module#method_Added
https://ruby-doc.org/core-2.2.2/Module.html#method-i-method_ added
module Thing
def self.method_added(method_name)
puts "Thing added #{method_name}"
end
def self.a_class_method; end
def do_something; end
end
class Person
def self.method_added(method_name)
puts "I added #{method_name}"
end
attr_accessor :name
end
Thing
Person.new
# Thing added do_something
# I added name
# I added name=
这篇关于用于定义方法的 Ruby 钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!