什么"self.included(base)"在模块中意味着什么?

Use*_*159 3 ruby

我看到这种模式很多:

module Article::Score

    def self.included(base)
        base.send :extend, ClassMethods
        base.send :include, InstanceMethods
    end

    module ClassMethods
    ...
    end

    module InstanceMethods
    ...
    end
end
Run Code Online (Sandbox Code Playgroud)

然后在文章模型中,我看到了这一点

class Article
   include Article::Score
   ...
end
Run Code Online (Sandbox Code Playgroud)

所以我的猜测是"base"可能是指文章类,我们只是包含实例方法并扩展类方法.但有人可以解释片段"self.included(base)"并概述那里发生了什么?

PPP*_*PPP 7

包含模块时会调用self.included函数.它允许在基础(包含模块的位置)的上下文中执行方法.