Andrei Horak Andrei Horak - 1 year ago 62
Ruby Question

How does precedence apply on an expression with an assignment and a conditional?

I have the following simple snippet:

var = 1 if false

I would expect this to evaluate as:

(var = 1) if false

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

What am I missing here?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download