title: Getting a list of classes that include a module
body: <p>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:</p>
  
  <pre><code>module MyModule
    def self.included(base)
      @classes ||= []
      @classes &lt;&lt; base.name
    end
  
    def self.classes
      @classes
    end
  end
  
  class MyClass
    include MyModule
  end
  </code></pre>
  
  <p>This works pretty well:</p>
  
  <pre><code>&gt; MyModule.classes #=&gt; nil
  &gt; MyClass.new #=&gt; #&lt;MyClass ...&gt;
  &gt; MyModule.classes #=&gt; ["MyClass"]
  </code></pre>
  
  <p>Now, I would like to extract this part out into a separate module that can be included in my other mixins. So, I came up with the following:</p>
  
  <pre><code>module ListIncludedClasses
    def self.included(base)
      p "...adding #{base.name} to #{self.name}.classes"
  
      @classes ||= []
      @classes &lt;&lt; base.name
  
      base.extend(ClassMethods)
    end
  
    def self.classes
      @classes
    end
  
    module ClassMethods
      def included(module_base)
        p "...adding #{module_base.name} to #{self.name}.classes"
  
        @module_classes ||= []
        @module_classes &lt;&lt; module_base.name
        super(module_base)
      end
      def classes
        @module_classes
      end
    end
  
  end
  
  module MyModule
    include ListIncludedClasses
  end
  </code></pre>
  
  <p>This doesn't work though, because the #included(module_base) method being added to MyModule from ListIncludedClasses is never getting run. Interestingly enough, it does successfully add #classes to MyModule.</p>
  
  <pre><code>&gt; MyModule.classes #=&gt; 
    "...adding Rateable to ListIncludedClasses.classes"
    =&gt; nil 
  &gt; ListIncludedClasses #=&gt; ["MyModule"]
  &gt; MyClass.new #=&gt; #&lt;MyClass ...&gt;
  # ^^ THIS SHOULD BE ADDING MyClass TO MyModule.classes ^^
  &gt; MyModule.classes #=&gt; nil
  </code></pre>
  
  <p>What am I missing?</p>