title: Understanding Ruby Nested Functions
body: <p>I am learning ruby at the moment. I am trying to understand the way closures work, and how they are different from functions. <strong>I am fully aware that closures should be implemented via proc or lambda</strong>.</p>
  
  <p>I am trying to get an in depth understanding of ruby. As such I checking all kinds of unorthodox code. I am trying to understand why line 3 works while line 5 is an error.</p>
  
  <pre><code>x=123
  def b(x)
      p x
      def a(u)
        p x # why is this an error?!?!?
      end
      a 4
  end
  
  b 1
  </code></pre>
  
  <ol>
  <li>If a can't access b's parameters, why doesn't it access the global x=123?</li>
  <li>Why does this work if I explicitly use change lines 1 &amp; 5 to the global "$x"?</li>
  <li>Why does this work if I use a lambda explictly?</li>
  </ol>
  
  <p>This is purely a learning exercise, I am doing this to understand what is going on beneath the hood.</p>