Constants in Ruby

Juzer Shakir
3 min readJun 30, 2021

--

Exploring an object that's rarely used explicitly by programmers.

A Constant is a placeholder that represent values such as string, integer, float, complex number, boolean value, array & hash. It’s used for values that aren’t supposed to change in program runtime.

Structure of this Article

To define a constant, the initial letter should be uppercase.

View Raw

The name can also contain all uppercase letters or numbers but no special characters.

What happens if we change the value of a constant?
Ruby allows us to change and modify the value of a constant, but with a catch!

View Raw

It gives us 2 warnings:

  1. The constant is already initialized.
  2. And the line number it was initialized at.

It is best practice to avoid modifying and changing values of constant in program runtime, if it has to change then its a best practice to use a variable instead.

To prevent modifying the value of a constant in runtime, the .freeze method is useful:

View Raw

Using .freeze method helps us prevent the modification of the value but we can still change its value. Ruby will run a warning message when we initialize a new value to an existing constant.

View Raw

Scope, Access & Initialization

If a constant is initiated in the root program, then they’re available to all methods, classes, and modules in a program.

We cannot initiate a constant inside a method, hence we need to initiate them before initiating the method inside the root program.

View Raw

We can however initiate constants inside a module or class and access them like: Class::Constant or Module::Constant.

View Raw

If constants are initiated in a specific class or module, then they’re accessible to self only and can’t be accessed outside of their scope.

Tip:
Its however best practice to initiate constants on top level with respect to where they’re defined so that its easily accessible and easy to debug.

FUN FACT:

All Classes and Modules are Constants too. Hence, their initial letters are always capital. You can verify this by giving name as lowercase initial letter, and see what error message you get!!

Variables vs Constants

So next time you would store a value in a program which one would you choose? A constant or a variable? Let’s see the key differences between them to get an idea of when to use which?

Variables:

  1. Starts with a lowercase letter.
  2. It can be defined anywhere.
  3. It cannot access variable value (if initiated in root) in all methods, classes, or modules, unless using the Global variable.
  4. No warning message while changing a variable’s value.

Constants:

  1. Starts with an uppercase letter.
  2. It cannot be defined inside a method.
  3. Can access constants in all methods, classes, or modules.
  4. Gives a warning message while changing or modifying a constant’s value.

Any suggestions or tips on this topic would be highly appreciated. Any queries? Ask Me!

My previous article: Variables in Ruby
My next article:
Block, Proc & Lambda

GitHub | LinkedIn

--

--