有一个名为MyModule
的模块:
module MyModule
extend ActiveSupport::Concern
def first_method
end
def second_method
end
included do
second_class_method
end
module ClassMethods
def first_class_method
end
def second_class_method
end
end
end
当某个类include
这个模块时,它会有两个实例方法(first_method
和second_method
)以及两个类方法(first_class_method
和second_class_method
) - 这很清晰。据说,
这到底是什么意思?也就是说,这个方法(
included
块将在包含该模块的类上下文中执行。
second_class_method
)何时被执行呢?
second_class_method
实现为raise "When am I raised?"
,那么它什么时候会被触发? - Andrey Deineko