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


