Getting a list of classes that include a module

前端 未结 3 520
离开以前
离开以前 2020-12-20 11:31

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following:

module MyModule
  def         


        
3条回答
  •  没有蜡笔的小新
    2020-12-20 12:04

    module MyMod; end
    
    class A; include MyMod; end
    class B < A; end
    class C; end
    
    ObjectSpace.each_object(Class).select { |c| c.included_modules.include? MyMod }
      #=> [B, A]
    

提交回复
热议问题