title: How do I find a method's nesting in Ruby?
body: <p>In Ruby, constant lookup is affected by nesting and methods retain their nesting.</p>
  
  <p>For example, if I have these modules:</p>
  
  <pre><code>module A
    X = 1
  end
  
  module B
    X = 2
  end
  
  module Foo
  end
  </code></pre>
  
  <p>I can define a method <code>Foo.a</code> that has a nesting of <code>[A, Foo]</code>:</p>
  
  <pre><code>module Foo
    module ::A
      Module.nesting #=&gt; [A, Foo]
      def Foo.a
        X
      end
    end
  end
  
  Foo.a #=&gt; 1
  </code></pre>
  
  <p>And a method <code>Foo.b</code> that has a nesting of <code>[Foo, B]</code>:</p>
  
  <pre><code>module B
    module ::Foo
      Module.nesting #=&gt; [Foo, B]
      def Foo.b
        X
      end
    end
  end
  
  Foo.b #=&gt; 2
  </code></pre>
  
  <p>The difference becomes apparent if I define <code>Foo::X</code>:</p>
  
  <pre><code>Foo::X = 3
  
  Foo.a #=&gt; 1  &lt;- still resolves to A::X
  Foo.b #=&gt; 3  &lt;- now resolves to Foo::X
  </code></pre>
  
  <p>But how do I determine the nesting of a given method?</p>