title: Undefined local variable based on syntax in Ruby
body: <p>In the following Ruby code,</p>
  
  <pre><code>#! /usr/bin/env ruby
  x = true
  y = x and z = y
  puts "z: #{z}"
  </code></pre>
  
  <p>It will output <code>z: true</code>, as expected.</p>
  
  <p>But in the following one, which I expect to have the same behavior:</p>
  
  <pre><code>#! /usr/bin/env ruby
  x = true
  z = y if y = x
  puts "z: #{z}"
  </code></pre>
  
  <p>It results in</p>
  
  <blockquote>
    <p>undefined local variable or method 'y' for main:Object (NameError)</p>
  </blockquote>
  
  <p>Why is that? </p>
  
  <p>I understood I am doing an assignment, and implicitly check for the assignment value to determine whether to run <code>z = y</code>. I also understood that if I add declaration of y, <code>y = nil</code>, right after the <code>x = 5</code> line, it will pass and run as expected. </p>
  
  <p>But isn't it correct to expect that the language should evaluate the <code>if</code> part first and then its content, and second chunk of code to behave the same as the first chunk of code?</p>