liveBook · Manning

Constant

As explained in The Well-Grounded Rubyist, Fourth Edition

A constant is a Ruby identifier whose name begins with an uppercase letter and conventionally denotes a value intended not to be reassigned.

A shared list for a class

class Ticket VENUES = ["Convention Center", "Fairgrounds", "Town Hall"] end Ticket::VENUES retrieves the list from outside the class. Inside Ticket, an instance method can refer to VENUES directly—for example, to check whether a proposed venue is in the list. section 3.7.1

A constant name begins with an uppercase letter. Common forms include FirstName and FIRST_NAME. Constants name classes as well as values such as VENUES; they often appear near the top of a class or module definition. The double colon in Ticket::VENUES identifies which class contains the constant. The same notation retrieves predefined values such as Math::PI. section 1.5 section 3.7.1

“Constant” expresses an intention, not an absolute restriction. Assigning A = 1 and then A = 2 succeeds, but Ruby warns that A was already initialized; reassignment is best avoided. By contrast, Ticket::VENUES << "High School Gym" changes the array without reassigning VENUES or producing that warning. The distinction is between changing which object a name refers to and changing the state of that object. section 3.7.2

A constant’s name alone does not identify it everywhere. Two enclosing classes or modules can each contain an X with a different value. A path such as M::C::D::N::X reaches a particular X; within M::C, D::N::X can be sufficient because lookup is relative to where it appears. Prefix a path with :: when lookup must begin at the top level—for example, ::String where a nearer String would otherwise be found. section 5.2.4

module M
  class C
    X = 2
    class D
      module N
        X = 1
      end
    end
  end
end
puts M::C::D::N::X        1
puts M::C::X          2

Compare the two X definitions and their full paths: identical constant names can resolve to different values in different nesting. section 5.2.4

When inspecting a running program, Ruby can list the constants of a class or module. That offers another way to check what names are available alongside reading their paths in the code. section 15.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through assignment, reassignment warnings, object mutation, and relative versus absolute lookup.
Examples
Uses a class’s venue array, Math::PI, and nested X definitions to illustrate storage and lookup.
What it assumes
Assumes familiarity with assignment and class definitions; introduces constant naming and double-colon lookup through small examples.
What you can do after
Evaluate/understand Ruby constant lookup and distinguish reassignment warnings from changes to referenced objects.
How it connects
Connects constants to classes, modules, identifier scope, object mutation, and runtime introspection.

Also explained in

Compare how 3 books explain constant →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.