是否有一种方法可以定义类似于Module#included的模块回调,但在接收器的定义结束后被称为?
例如,为了能够访问在包含后定义的接收者方法:
module A
def self.included(base)
if base.respond_to? :foo
puts ":foo is defined in #{base}"
else
puts ":foo is not defined in #{base}"
end
end
end
class B
include A
def self.foo ; end
end
# :foo is not defined in B
可以将模块包含在接收者定义的末尾,但感觉不那么惯用:
class C
def self.foo ; end
include A
end
# :foo is defined in C