Updated April 6, 2023
Introduction to Ruby Constants
In Ruby, Constant is another type of variable, the only difference is that we cannot change the value of the constant throughout the program. In case if we try to change the value of the variable it will raise a warning error. In Ruby, constants always start with a capital letter, but you will see many programmers prefer to write all letters of the program like the capital letters only, the main benefit of constant in Ruby if we wanted some variable to be same for any particular program so that all other methods and class can use the same value we define variables as the constant.
List all Ruby Constants:
Before going to understand various lists of the constant in the Ruby we need to understand a few points.
- Constants are the variable which once we define any variable with the capital letter the compiler will consider it as the constant and in case if we try to make any change in the constant it will throw a warning.
- We can list any constant in Ruby into categories on is constant defined inside the class which can be accessed outside of class with the:: operator and another is the constant which we have defined at the top of the program which can be accessed by any class and method.
- We can say constant at the top of the program as a global constant.
- An example of a global constant is the environment variable for the whole program.
Examples to Implement of Ruby Constants
Let us see some examples of lists of constants in Ruby:
Example #1
An example where we are looking at constants defined at the top of the program and way of using it inside and outside of the program.
- First, we have defined a global constant named as the speak. This constant will be available for all classes and methods directly with the name of the constant.
- Next, we have created a class with the name Human which is going to use the constant.
- Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize.
- We wrote a method display_human_data inside the class which will display the data for the attributes passed to it.
- Next, we are creating an object from the class Human and calling method display_human_data with the required parameters to display the user data.
Please see the below example.
Code :
SPEAK="speak"
class Human
# Method for initialisation inside the class
definitialize(name ,age ,gender )
# Initialising the variables
@name = name
@age = age
@gender =gender
end
defdisplay_human_data()
puts "The name of the user is #{@name} and age is #{@age} also he is a #{@gender}"
end
defdisplay_common()
puts "Human can #{SPEAK}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new("ranjan", 31, "male")
object2 = Human.new("anjali", 28, "female")
object1.display_human_data()
object1.display_common()
object2.display_human_data()
object2.display_common()
puts SPEAK
Output:
Example #2
In this example, we are focusing on the constant defined inside the class and the constant defined outside the class and also the way of use for the constant types.
- First, we have defined a global constant named as the speak. This constant will be available for all classes and methods directly with the name of the constant.
- Then inside the class we have taken another constant called WALK.
- Next, we have created a class with the name Human.
- Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize.
- We wrote a method display_human_data inside the class which will display the data for the attributes passed to it.
- Next, we are creating an object from the class Human and calling method display_human_data with the required parameters to display the user data.
- Outside of the class we are able to access directly the constant which we have defined at the top of the program and to access the constant of the class we need to use :: operator.
Please see the below example:
Code:
SPEAK="speak"
class Human
WALK ="walk"
# Method for initialisation inside the class
definitialize(name ,age ,gender )
# Initialising the variables
@name = name
@age = age
@gender =gender
end
defdisplay_human_data()
puts "The name of the user is #{@name} and age is #{@age} also he is a #{@gender}"
end
defdisplay_common()
puts "Human can #{SPEAK} and #{WALK}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new("ranjan", 31, "male")
object2 = Human.new("anjali", 28, "female")
object1.display_human_data()
object1.display_common()
object2.display_human_data()
object2.display_common()
puts SPEAK
puts Human::WALK
Output:
Example #3
This example contains a mixture of both constant inside the class and constant outside the class.
- First, we have defined a global constant named as the speak. This constant will be available for all classes and methods directly with the name of the constant.
- Then inside the class we have taken another constant called WALK and THINK.
- Next, we have created a class with the name Human.
- Next, we have created a Ruby inbuilt method initialize which will be used as the constructor inside the Ruby class to initialize.
- We wrote a method display_human_data inside the class which will display the data for the attributes passed to it.
- Next, we are creating an object from the class Human and calling method display_human_data with required parameters to display the user data.
- We have also created a method called display_uniqueto display the constant unique things of humans.
Please see the below example.
Code:
SPEAK="speak"
class Human
WALK ="walk"
THINK="think"
# Method for initialisation inside the class
definitialize(name ,age ,gender )
# Initialising the variables
@name = name
@age = age
@gender =gender
end
defdisplay_human_data()
puts "The name of the user is #{@name} and age is #{@age} also he is a #{@gender}"
end
defdisplay_common()
puts "Human can #{SPEAK} and #{WALK}"
end
defdisplay_unique()
puts "Some Human can #{THINK}"
end
end
# Creating an objects and passing parameters for initialization
object1 = Human.new("ranjan", 31, "male")
object2 = Human.new("anjali", 28, "female")
object1.display_human_data()
object1.display_common()
object1.display_common()
object2.display_human_data()
object2.display_common()
object2.display_unique()
puts SPEAK
puts Human::WALK
puts Human::THINK
Output:
Conclusion
From this tutorial we learned the basic concept of the Constants in Ruby, we learned about the importance of uses of constants in Ruby. We saw various examples that were describing the list of the constants which we can have in Ruby.
Recommended Article
We hope that this EDUCBA information on “Ruby Constants” was beneficial to you. You can view EDUCBA’s recommended articles for more information.