title: How `self` works in nested methods in ruby?
body: <p>To see who is playing the role of <code>self</code> in <code>nested methods</code>, I tried the below code:</p>
  
  <pre><code>def test
    p "#{self}"
    def show
      p "#{self}"
    end
  end
  # =&gt; nil
  </code></pre>
  
  <p>As an effect, I got the two objects below:</p>
  
  <pre><code>Object.new.test
  "#&lt;Object:0x00000002212d78&gt;"
  # =&gt; nil
  Object.new.test.show
  "#&lt;Object:0x00000002205330&gt;" #&lt;~~~ this is self understood
  "" #&lt;~~~ how this one came?
  # =&gt; ""
  </code></pre>
  
  <p>But from the encoded numbers, I couldn't understand which class those objects belong to. And I tried the code below and got the respective <code>class</code> names.</p>
  
  <pre><code>Object.new.test.class
  "#&lt;Object:0x000000021ff3b8&gt;"
  # =&gt; NilClass
  Object.new.test.show.class
  "#&lt;Object:0x000000020660b0&gt;"
  ""
  # =&gt; String
  </code></pre>
  
  <p>So can anyone help me to understand the concept of how the above code produced those <code>class</code> names?</p>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>Here I tried to ask my question on more specific way:</p>
  
  <pre><code>def test
  p "First level # =&gt; #{self}"
  def show
  p "Second level # =&gt; #{self}"
  end
  end
  # =&gt; nil
  Object.new.test.show
  "First level # =&gt; #&lt;Object:0x000000014a77b0&gt;"
  "Second level # =&gt; "
  # =&gt; "Second level # =&gt; "
  Object.new.test.show.class
  "First level # =&gt; #&lt;Object:0x0000000130ef70&gt;"
  "Second level # =&gt; "
  # =&gt; String
  </code></pre>
  
  <p><em><strong>Why the <code>p "Second level # =&gt; #{self}"</code> statement <code>self</code> has <code>""</code> value?</em></strong></p>