Consider the following irb snippet from a freshly-started session:
irb:01> baz # => NameError, baz is not defined
irb:02> baz || baz = 0 # => NameError, baz is not defined
irb:03> baz # => nil
baz
was an undefined variable and trying to evaluate it produced a NameError
. Yet, somehow, after this operation, baz
was defined, and has a value of nil
. Seemingly, the value nil
was assigned to the variable baz
even though no one (explicitly) asked for it to be. Is there an underlying language reason why this behavior is desirable?
What is the rule that explains this behavior and other similarly confusing constructs, such as these:
irb:04> true if foo # => NameError
irb:05> foo # => NameError; name still undefined
irb:06> foo = (true if foo) # => nil
irb:07> foo # => nil; name defined as nil
irb:08> true || i = 0 || j = 2 # => i and j are nil; || appears nonlazy
irb:09> raise || quux = 1 # => RuntimeError, quux is nil