0

I have the following simple snippet:

var = 1 if false

I would expect this to evaluate as:

(var = 1) if false

so var would be undefined. However, var gets defined and receives a nil as its value.

What am I missing here?

linkyndy
  • 17,038
  • 20
  • 114
  • 194

2 Answers2

1

Ruby recognizes local variables during parsing. So, in your case, even thouogh it's not set to 1 (because the precedence of this expression is like you wrote), ruby knows that it's local variable and doesn't raise NameError.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
1

Ruby parser defines var when it sees it on the lefthand side of an expression (even though its inside of a conditional that doesn’t run). So nil looks an appropriate value.

Ursus
  • 29,643
  • 3
  • 33
  • 50