When the Ruby parser sees the sequence identifier, equal-sign, value,
as in this expressionx = 1
it allocates space for a local variable called
x
. The creation of the
variable—not the assignment of a value to it, but the internal
creation of a variable—always takes place as a result of this kind of
expression, even if the code isn’t executed! Consider this example:if false x = 1 end p x # Output: nil p y # Fatal Error: y is unknown
The assignment to
x
isn’t executed, because it’s wrapped in a failing
conditional test. But the Ruby parser sees the sequencex = 1
, from
which it deduces that the program involves a local variablex
. The
parser doesn’t care whetherx
is ever assigned a value. Its job is
just to scour the code for local variables for which space needs to
be allocated. The result is thatx
inhabits a strange kind of variable limbo.
It has been brought into being and initialized tonil
.
In that respect, it differs from a variable that has no existence at
all; as you can see in the example, examiningx
gives you the value
nil
, whereas trying to inspect the non-existent variabley
results
in a fatal error. But althoughx
exists, it has not played any role in
the program. It exists only as an artifact of the parsing process.
Well-Grounded Rubyist chapter 6.1.2