我看到这种模式很多: module Article::Score def self.included(base) base.send :extend, ClassMethods base.send :include, InstanceMethods end module ClassMethods ... end module InstanceMethods ... endend 然后在文章模型中,我看
module Article::Score
def self.included(base)
base.send :extend, ClassMethods
base.send :include, InstanceMethods
end
module ClassMethods
...
end
module InstanceMethods
...
end
end
然后在文章模型中,我看到了这一点
class Article include Article::Score ... end
所以我的猜测是“base”可能是指文章类,我们只是包含实例方法并扩展类方法.但有人可以解释片段“self.included(base)”并概述那里发生了什么?
包含模块时会调用self.included函数.它允许在基础(包含模块的位置)的上下文中执行方法.