I am getting NameError: undefined local variable or method with ruby 2.1.2
As observed in this question, expressions like:
bar if bar =
My answer is based on Ruby 2.1.2.
Adding with @Jörg W Mittag answer.
Another commonly confusing case is when using a modifier if:
p a if a = 0.zero? # => NameError: undefined local variable or method `a'
Rather than printing “true” you receive a NameError, “undefined local variable or method `a’”. Since ruby parses the bare a left of the if first and has not yet seen an assignment to a it assumes you wish to call a method. Ruby then sees the assignment to a and will assume you are referencing a local method.
The confusion comes from the out-of-order execution of the expression. First the local variable is assigned-to then you attempt to call a nonexistent method.
Based on above explanation -
bar if bar = false
simply returns nil, as the expression has been evaluated as false, the body of the code associated with the if modifier wouldn't be executed. nil is being returned by any block in Ruby, by default when there is no explicit default value.