title: Why doesn't the fallback mechanism work here?
body: <p>When accessing a variable, the fallback mechanism in Ruby first looks up local variables, and if it doesn't find one, it automatically applies <code>self</code> and looks up instance variables. However, the following code does not work: </p>
  
  <pre><code>class My
    def init
      @abc = "abc"
    end
  
    def pt
      puts abc
    end
  end
  </code></pre>
  
  <p>I got this error message when I'm trying to call <code>pt</code> on an instance:</p>
  
  <pre><code>2.0.0-p247 :009 &gt; my = My.new
   =&gt; #&lt;My:0x007f9b5a1b1000&gt; 
  2.0.0-p247 :010 &gt; my.init
   =&gt; "abc" 
  2.0.0-p247 :011 &gt; my.pt
  NameError: undefined local variable or method `abc' for #&lt;My:0x007f9b5a1b1000 @abc="abc"&gt;
  </code></pre>
  
  <p>However, <code>@abc</code> DOES exist as an instance variable in the object:</p>
  
  <pre><code>2.0.0-p247 :012 &gt; my.instance_variables
  =&gt; [:@abc] 
  </code></pre>
  
  <p>So why can't <code>pt</code> find <code>abc</code> here? Shouldn't it automatically look-up instance variables, since it's not defined locally, and print it? </p>
  
  <p>NOTE: </p>
  
  <p>I know using <code>puts @abc</code> would work, but this is NOT the point of my question. My question is about the fallback mechanism in Ruby. This code works: </p>
  
  <pre><code>2.0.0-p247 :079 &gt; class My
  2.0.0-p247 :080?&gt;     def initialize(param)
  2.0.0-p247 :081?&gt;         @abc = param
  2.0.0-p247 :082?&gt;       end
  2.0.0-p247 :083?&gt;   
  2.0.0-p247 :084 &gt;       def printabc
  2.0.0-p247 :085?&gt;         puts abc
  2.0.0-p247 :086?&gt;       end
  2.0.0-p247 :087?&gt;   end
  
  2.0.0-p247 :089 &gt; My.new("haha").printabc
  haha
  </code></pre>
  
  <p>I don't know why it doesn't work in the previous case but works in the latter. </p>