Because the assignment of x happens and it's not lost...

puts "x before all: "
puts "#{x.inspect}" rescue puts "x is nil"
puts "x before the if: "
puts "#{x.inspect}" if(x = 2) rescue puts "x is still nil"
puts "but x after the if: #{x.inspect}"

On Sat, Feb 8, 2014 at 5:13 PM, Arup Rakshit <lists / ruby-forum.com> wrote:
> Edoardo Rossi wrote in post #1136041:
>> Hi Arup.
>
>> if() when used as a modifier has its own context... its scope.
>> So the assignment is never seen outside of the if and 'bar' it's
>> undefined.
>
> See another snippet :
>
>>> puts "hi" if x = 2
> (pry):11: warning: found = in conditional, should be ==
> hi
> => nil
>>> x
> => 2
>>>
>
> How then here `x` does print **2** ?
>
> --
> Posted via http://www.ruby-forum.com/.



-- 


Eddie