Reason #4 • January 4th, 2026

Declaring constants

In Ruby, constants are used to store values that should not change throughout the execution of a program. Constants are defined just like variables but start with an uppercase letter. By convention, constants are usually written in all uppercase letters with words separated by underscores.

Ruby
PI = 3.14159
MAX_USERS = 100

AlsoAConstant = "But no one does this"

# Redefining constants is possible but prints:
# Warning: already initialized constant MAX_USERS
MAX_USERS = 200
      
JavaScript
const PI = 3.14159;
const MAX_USERS = 100;
const alsoAConstant = "In JS, casing is flexible and depends on preference";
// Reassigning a constant will throw an error
MAX_USERS = 200; // Uncaught TypeError: Assignment to constant variable.
      

Since constants always start with an uppercase letter, it is never ambiguous to the reader whether a name refers to a constant or a variable. But there are some more interesting ideas to be derived from this example.

First, Ruby allows reassigning constants but warns the developer, while most languages strictly prohibit it. This reflects different philosophies: Within the Ruby runtime, nothing is fixed and everything is allowed to change and evolve.

This doesn't mean that well-behaved Ruby developers would ever entertain the idea of reassigning constants in a production environment. But this flexibility can be very useful during development and testing. E.g. you might temporarily change a constant's value to test different scenarios. Or you might redefine a constant in an interactive Ruby session (IRB) while experimenting with code.

Second, while PascalCase is not typically used for declaring simple constants, it is used for classes and modules. When we define a class or module we must use constant naming conventions, as they are constants in Ruby.

Ruby
class MyClass
  def initialize(name)
    @name = name
  end
end

# is the same as:
MyClass = Class.new do
  def initialize(name)
    @name = name
  end
end

# we can define classes fully uppercased too, but it's not idiomatic:
class MY_CLASS
end

# declaring a class with a lowercase name raises an error:
class my_class
end
# syntax errors found (SyntaxError)
# > 2 | class my_class
#     |       ^~~ unexpected constant path after `class`; class/module name must be CONSTANT
#   4 | end

# but should we want a temporary class assigned to a regular variable we sure can:
my_class = Class.new do
end

# all the same rules apply to modules, you can think of them as constant namespaces:
module MyModule
  CONSTANT_IN_MODULE = 42

  class InnerClass
  end
end

puts MyModule::CONSTANT_IN_MODULE # => 42
    

Spoiler alert: we'll talk more about what's going on here and how "everything is an object" in Ruby another day!