title: Where do methods defined at the top-level exist?
body: <p>I am learning some metaprogramming and I was stuck trying to find a method. Let's say I have the following class:</p>
  
  <pre><code>class MyClass
    def self.my_method
    end
    def your_method
    end
  end
  </code></pre>
  
  <p>With the following code I can search for each method in the object space:</p>
  
  <pre><code>type = Class
  name = /^my_method$/
  result = ObjectSpace.each_object(type).select do |o|
    o.instance_methods(false).grep(name).size &gt; 0 || o.methods.grep(name).size &gt; 0
  end
  p result
  </code></pre>
  
  <p>And it finds it showing the following output:</p>
  
  <pre><code>[MyClass]
  </code></pre>
  
  <p>As the searcher code also searches for instance methods, it shows the same output when looking for your_method.</p>
  
  <p>Even with if I add a singleton method to an object:</p>
  
  <pre><code>mc = MyClass.new
  def mc.our_method
  end
  </code></pre>
  
  <p>Just changing the searcher like this:</p>
  
  <pre><code>type = Object
  name = /^our_method$/
  result = ObjectSpace.each_object(type).select do |o|
    o.methods.grep(name).size &gt; 0
  end
  p result
  </code></pre>
  
  <p>It also finds it:</p>
  
  <pre><code>[#&lt;MyClass:0x8f86760&gt;]
  </code></pre>
  
  <p>The question is, how do I find a method defined in the top level object? This method:</p>
  
  <pre><code>def hidden
  end
  </code></pre>
  
  <p>Besides, which is the current class when defining a method like this?</p>