Constants, globals and scope

Constants and the warning

A name that starts with a capital is a constant: Ruby lets a program assign it once and warns when a second assignment replaces the value. The warning is the feature; the program keeps running with the new value.

ruby -e 'X = 1; X = 2; puts X'

-e:1: warning: already initialized constant X
-e:1: warning: previous definition of X was here
2

The lookup goes outward: a constant written inside a class is looked up in the class, then in the enclosing modules, then at the top level. That is why VERSION inside a gem module finds the constant of the module and not one from another gem with the same name.

The globals Ruby defines

Ruby has a handful of global variables that are always there. The four a script uses:

ruby -e 'puts $PROGRAM_NAME; puts $0 == $PROGRAM_NAME'

-e
true

Write your own globals only in a scratch script. A library that sets $verbose or $debug changes the behaviour of everything that shares the process, and a name collision is impossible to see in a diff.

ENV is not a global

Environment variables look like globals and are not: they live in the process environment and Ruby exposes them through the ENV object, which is a hash-like constant.

ruby -e 'puts ENV.fetch("HOME")'

/home/agent

fetch raises when the variable is missing, which is what a required setting wants. ENV["MODE"] returns nil instead, and ENV.fetch("MODE", "development") gives a default. Lesson 11.3, later in the course, has the whole configuration lesson.

What to watch for

A constant is not frozen: COLORS = [] can still be pushed to, and every caller sees the change. Freeze the value when it is meant to be read-only, as in lesson 2.7.

The reassignment warning appears by default; -W0 silences every warning, and Warning[:deprecated] does not touch this one. A second assignment to a constant is almost always a bug or a rename that missed a line.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What happens when a constant is assigned a second time?

    Answer

    Ruby warns and keeps running with the new value

    The warning says the constant was already initialized, and the new value wins.

  2. What does ruby -e 'X = 1; X = 2; puts X' print on stdout?

    Answer

    2

    The warnings go to stderr, and the program prints the new value.

  3. Where does a constant inside a class start its lookup?

    Answer

    In the class, then the enclosing modules, then the top level

    The search goes outward from the lexical scope, which is why a gem module finds its own VERSION.

  4. Which global holds the name of the running program?

    Answer

    $PROGRAM_NAME, also $0

    $0 is the name of the program; under -e it is the string -e.

  5. What does this program print? puts $PROGRAM_NAME

    Answer

    -e

    A one-liner has no file name, so the program name is the -e option.

  6. Which global holds the last exception raised?

    Answer

    $!

    $! is the exception that rescue without an argument reads.

  7. How does Ruby expose the environment variables?

    Answer

    Through the ENV object, a hash-like constant

    ENV behaves like a hash of names to values.

  8. What does ENV.fetch("MODE") do when MODE is not set?

    Answer

    Raises KeyError

    fetch without a default raises, which is what a required setting wants; ENV["MODE"] returns nil instead.

  9. What does ENV.fetch("MODE", "development") do when MODE is not set?

    Answer

    Returns "development"

    The second argument is the default that fetch returns instead of raising.

  10. A constant holds an array. Can the array still be changed?

    Answer

    Yes: a constant is not frozen, so the array can be modified

    The name is constant, not the object; freeze the value when it must stay read-only.

Article author: Arthur Isaev