title: Why can I assign an undefined variable to itself in Ruby and get nil?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8908050/why-a-a-is-nil-in-ruby">Why a = a is nil in Ruby?</a>  </p>
  </blockquote>
  
  
  
  <p>There's a, shall we say, <em>"odd phenomenon"</em> in Ruby with using undefined variables. It's like this:</p>
  
  <pre><code># irb session follows
  #
  foo        # undefined local variable or method 'foo'
  bar        # same for 'bar'
  foo = bar  # still same for 'bar'
  foo = foo  # nil - HUH?
  foo        # is now set to nil!?
  </code></pre>
  
  <p><strong>Why can I assign an undefined variable to itself in Ruby and get <code>nil</code></strong>?</p>
  
  <p>Note that I'm using Ruby 1.9.3 here. I'm not sure what other versions this may be true in.</p>
  
  <p>(Thanks to Gary Bernhardt for demonstrating this in his <a href="https://www.destroyallsoftware.com/talks/wat" rel="nofollow noreferrer">hilarious talk</a>.)</p>