加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 模块中的“self.included(base)”是什么意思?

发布时间:2020-12-17 01:31:19 所属栏目:百科 来源:网络整理
导读:我看到这种模式很多: module Article::Score def self.included(base) base.send :extend,ClassMethods base.send :include,InstanceMethods end module ClassMethods ... end module InstanceMethods ... endend 然后在文章模型中,我看到了这一点 class Ar
我看到这种模式很多:

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函数.它允许在基础(包含模块的位置)的上下文中执行方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读