Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we will see how a constant behaves in Ruby. So let’s get started with the introduction to constants and their relation and difference with variables.
In Ruby, a constant is similar to a variable, except its value is expected to remain constant throughout the program. The Ruby interpreter does not enforce constant consistency, although it does send a warning if a program modifies a constant's value. Constant names are similar to the names of local variables in lexical similarity; the only difference is that they all start with a capital letter.
For example:
VALUE_OF_PI = 3.14 # Defining a Constant References in ruby.
Defining a constant
In this section, we will see how a constant is defined in ruby. So, are u excited? Let’s go.
Declaring a constant does not necessitate the use of any specific symbols or syntax. All you have to do now is change the initial letter to an uppercase letter.
Most constants are written in full uppercase with underscores between words as a matter of convention. Class and module names in Ruby are also constants. However, they are written in camel case with starting capital letters.
Constants have the visibility of global variables, despite their appearance of local variables with capital letters: they can be used anywhere in a Ruby application, regardless of scope. Constants, unlike global variables, can be specified by classes and modules and hence have qualified names.
The following are valid Constant References in ruby:
AXE = 1
PI = 3.14
Planck = 6.6
You can also try this code with Online Ruby Compiler
main.rb:2: warning: already initialized constant AXE
main.rb:1: warning: previous definition of AXE was here
2
From the example above, we saw that Ruby doesn’t stop us from changing the value of the constant. But, it throws a warning. Our program will continue to function normally, but we must avoid this.
We can create an immutable constant by using the freeze keyword.
Example:
PLAYER = "saurabh yadav".freeze
PLAYER << "o"
puts PLAYER
You can also try this code with Online Ruby Compiler
Variables in Ruby store data that may be utilized later in a program. Each variable serves as a memory and is given a unique name.\
There are four types of variables in Ruby:
Local variable
Class variable
Instance variable
Global variable
What is a global constant in Ruby?
The environment variable for the entire program is an example of a global constant. Let's look at some examples of Ruby constant lists: We'll look at constants defined at the top of the program and how to use them both inside and outside of it in this example.
What is the definition of a class constant in Ruby?
Class constants can be useful if you need to define some constant data within a class.
Constants of the class, the name of a constant begins with an uppercase letter. It should only be given a value once. Reassignment of a constant generates a warning but not an error in the current Ruby implementation (the non-ANSI version of eval .rb does not report the warning).
Is String Literal in Ruby Mutable?
The []= method in Ruby can be used to modify a portion of a string. To use this function, simply supply the string of characters to be replaced and assign the new string to the method.
Why are Ruby classes constant, and why is it important?
You're getting an " uninitialized constant " error because you most likely forgot to require a file or gem that defines the constant, you're getting an "uninitialized constant" error.
Conclusion
In this article, we have extensively discussed the concept of the Constant References in ruby. We started with the introduction of Constant References in ruby, uninitialized constant error, Constant method references in ruby, and scope of Constant References in ruby.
After reading about the Constant References in ruby, are you not feeling excited to read/explore more articles on the topic of ruby? Don't worry; Coding Ninjas has you covered. To learn, see String literal in Ruby, Constants in Ruby, and Methods in Ruby.