How to Define a Constant in Ruby
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
<NAME> = <value>
| Part | What 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:

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
ruby main.rb
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

The command is the same in zsh, bash and PowerShell.
What Happened
- The constants worked like variables:
ISSUERprinted its URL andMAX_RETRIES * 2printed6. - Assigning
ISSUERagain 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
| Mistake | What happens | Do this instead |
|---|---|---|
| Assigning a constant inside a method | The file doesn't run: dynamic constant assignment | Define constants at the top of the file, a class or a module |
Ignoring already initialized constant | The program runs with a different value than you expect | Find the second assignment and rename or remove it |
| Expecting a constant's object to be fixed | A constant holding a string or an array can still be changed in place | Call .freeze on the value when it must not change |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
warning: already initialized constant ISSUER | The constant was assigned a second time | Find the line the warning names |
dynamic constant assignment | A constant is assigned inside a method | Move it out of the method |
uninitialized constant ISSUER (NameError) | The constant is used before it's defined, or is misspelled | Define 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
- Variables, constants, and arguments, in the Ruby FAQ.
