title: How do I get constants defined by Ruby's Module class via reflection?
body: <p>I was trying to get Matz and Flanagan's "Ruby Programming Language" metaprogramming chapter into my head, However I couldn't understand the output from the following code snippet that I dreamed up:</p>
  
  <pre><code>p Module.constants.length           # =&gt; 88
  $snapshot1 = Module.constants       
  class A
    NAME=:abc
  
    $snapshot2 = Module.constants
    p $snapshot2.length               # =&gt; 90
    p $snapshot2 - $snapshot1         # =&gt; ["A", "NAME"]
  
  end
  p Module.constants.length           # =&gt; 89
  p Module.constants - $snapshot1     # =&gt; ["A"]
  p A.constants                       # =&gt; ["NAME"]
  </code></pre>
  
  <p>The book states that the class method <code>constants</code> returns the list of constants for the class (as you can see in the output for <code>A.constants</code>).
  I was trying to get the list of constants defined for the Module class when I came across the above strange behavior.     </p>
  
  <p><code>A</code>'s constants show up in Module.constants. How do I get the list of constants defined by the Module class?</p>
  
  <p>The <a href="http://ruby-doc.org/core/classes/Module.html#M001656" rel="noreferrer">docs</a> state </p>
  
  <blockquote>
    <p><code>Module.constants</code> returns all constants defined in the system. including names of all classes and methods</p>
  </blockquote>
  
  <p>Since <code>A</code> inherits its implementation from <code>Module.constants</code>, how does it behave differently in the base and derived types?</p>
  
  <pre><code>p A.class               # =&gt; Class
  p A.class.ancestors       # =&gt; [Class, Module, Object, Kernel]
  </code></pre>
  
  <p><em>Note: If you're using Ruby 1.9, <code>constants</code> would return an array of symbols instead of strings.</em></p>