Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`defined?` and `unless` not working as expected

I was expecting the following snippet:

var = "Not Empty" unless defined? var
var # => nil

to return "Not Empty", but I got nil. Any insight into why this is happening?

like image 402
Noman Ur Rehman Avatar asked Sep 29 '15 08:09

Noman Ur Rehman


2 Answers

This is one of the only moments in Ruby I would call actual WTFs.

You have to use

unless defined? var
  var = :value
end

With the postfix syntax, the interpreter will internally nil-ify the value so it can reason about the variable, thus making it defined before the check is done:

# Doesn't print anything
unless defined?(foo) and (p(foo) or true)
  foo = :value
end

# Prints nil
bar = :value unless defined?(bar) and (p(bar) or true)
like image 127
ndnenkov Avatar answered Sep 20 '22 18:09

ndnenkov


Local variables are defined (as nil) at the point they are parsed. Definition of var2 precedes the condition. That makes var2 defined even when if the assignment is not executed. Then, the condition evaluates that var2 is defined, which retains the value nil for var2.

like image 31
sawa Avatar answered Sep 18 '22 18:09

sawa