Skip to main content

How to Define a Constant in Ruby

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In Ruby, any name that starts with a capital letter is a constant: ISSUER = 'https://login.example.org'. Ruby still lets you assign it a new value, but it warns you with already initialized constant.

Syntax​

Syntax
<NAME> = <value>
PartWhat it does
<NAME>The constant's name. It must start with a capital letter, and by convention it's all capitals with underscores between words.
<value>The value the program uses everywhere and never changes.

The Ruby FAQ: "A constant is a variable whose name starts with an upper case letter. Constants may not be reassigned from within instance methods, but can otherwise be changed at will. When a constant is assigned a new value, a warning is issued."

The Example​

The example defines two constants, uses them, and then assigns a new value to one of them, to show what Ruby does about it.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which defines ISSUER and MAX_RETRIES, prints ISSUER and MAX_RETRIES times 2, then assigns a new value to ISSUER and prints it.

main.rb
ISSUER = 'https://login.example.org'
MAX_RETRIES = 3

puts ISSUER
puts MAX_RETRIES * 2

ISSUER = 'https://elsewhere.example.org'
puts ISSUER

Step 2: Run It​

Terminal
ruby main.rb
Output
https://login.example.org
6
main.rb:7: warning: already initialized constant ISSUER
main.rb:1: warning: previous definition of ISSUER was here
https://elsewhere.example.org

A zsh terminal where ruby main.rb prints the issuer and 6, then warns already initialized constant ISSUER and previous definition of ISSUER was here, and prints the new issuer.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • The constants worked like variables: ISSUER printed its URL and MAX_RETRIES * 2 printed 6.
  • Assigning ISSUER again worked, with two warnings: one on line 7, where it was assigned again, and one pointing back to line 1, where it was first defined.
  • The new value was used. Ruby warns about a changed constant but doesn't stop it.

The warning is your signal: a constant that changes is almost always a mistake, such as a file loaded twice or two places that use the same name.

Common Mistakes​

MistakeWhat happensDo this instead
Assigning a constant inside a methodThe file doesn't run: dynamic constant assignmentDefine constants at the top of the file, a class or a module
Ignoring already initialized constantThe program runs with a different value than you expectFind the second assignment and rename or remove it
Expecting a constant's object to be fixedA constant holding a string or an array can still be changed in placeCall .freeze on the value when it must not change

Troubleshooting​

SymptomLikely causeFix
warning: already initialized constant ISSUERThe constant was assigned a second timeFind the line the warning names
dynamic constant assignmentA constant is assigned inside a methodMove it out of the method
uninitialized constant ISSUER (NameError)The constant is used before it's defined, or is misspelledDefine it at the top of the file

FAQ​

Do constants have to be all capitals?​

Only the first letter has to be a capital. Issuer = 'x' is also a constant, but all capitals with underscores is the usual style, so readers can tell constants from class names.

Can I stop a constant from being reassigned?​

No: Ruby only warns. You can stop its value from being changed in place with .freeze, but the name can still be assigned again.

Conclusion​

A Ruby constant is any name that starts with a capital letter. Ruby warns when you assign it again, so treat that warning as an error to fix.

Additional Resources​