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

Ruby:获取扩展模块列表?

发布时间:2020-12-16 19:56:26 所属栏目:百科 来源:网络整理
导读:当您将模块包含在类或其他模块中时,您可以调用 @mymod.included_modules 获取包含的模块列表. 是否有等同的列出模块扩展的模块? module Feature1endmodule Feature2 extend Feature1endFeature2.extended_modules #= [Feature1] 解决方法 他们在那里,你只需
当您将模块包含在类或其他模块中时,您可以调用
@mymod.included_modules

获取包含的模块列表.

是否有等同的列出模块扩展的模块?

module Feature1
end

module Feature2
  extend Feature1
end

Feature2.extended_modules #=> [Feature1]

解决方法

他们在那里,你只需要看正确的地方:
(class << Feature2; self end).included_modules   # [Feature1,Kernel]

我们可以这样推广:

class Module
  # Return any modules we +extend+
  def extended_modules
    (class << self; self end).included_modules
  end
end

# Now get those extended modules peculiar to Feature2
Feature2.extended_modules - Module.extended_modules # [Feature1]

(编辑:李大同)

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

    推荐文章
      热点阅读