Lets discuss another container object present in Ruby. This one is called a constant.
You may have read our posting on variables and figured we were done with these container objects. True, a variable is a container as well.
Why then do we need two container objects?.. As previously mentioned, a variable's name in itself is a clue. What is the definition of variable?
var·i·a·ble pronounced /ˈve(ə)rēəbəl/ :
Noun: An element, feature, or factor that is liable to vary or change.
Adjective: Not consistent or having a fixed pattern; liable to change.
To summarize a variable is defined as subject to change, or not static. Conversely how would one define the word constant?
con·stant pronounced /kon-stuhnt/ :
Noun: Something that does not or cannot change or vary.
Adjective: Not changing or varying; uniform; regular; invariable.
So a constant appears to be the opposite in definition to a variable. As this holds true in the English language, it also holds true in the Ruby Programming language.
A constant is data that will not (should not) change. (If your constants change they should be variables). You will also be warned by the Ruby interpreter when redefining a constant value.
Constants and real world usage
Why do you need to have a container object for data that does not change? A very good example of the answer why, is the mathematical constant Pi (π). Rounded down to 2 the hundredth, Pi is equal to 3.14.
You could simply create a variable to hold this value however that doesn't scale well, it also is not logical. Why would you store a value which is static in a container object whose value intended or at the bare minimum expected to change. A better convention is to keep this data as a constant value in your program.
Ruby and it's standard library comes with many Modules and Classes, each of these modules and classes have their own variables and constants. Looking at our example with Pi (π), we can map this one to one with Ruby programming. Included in the standard library is the Math module. The Math module as so aptly named, provides variables, constants and methods which allow one to manipulate numbers and perform mathematical calculations in an object oriented manner.
The value of Pi, is actually kept as a constant in the Math module. Lets see what is the value of this constant.
#Typing the below into the IRB console returns a Float object.
This number may look a little different than the commonly seen (and notoriously rounded down) 3.14. This is simply because humans do not deal with such large numbers as easily as a computer will. Let me show you a quick way to make this look like what we are used to.
#We call on the round method of a Float object. This method is able to round a number up or down accordingly, it takes an optional argument of precision.
So back to constants....
The Pi value does NOT change, EVER. It is a static value in mathematical operations across the world, if it is changing or is different from the previously shown value, one of the below may be the case:
You may have noticed that when I have defined a class in my code, the class is following the same convention as a constant. With the first letter always capitalized. This is because a class is considered a constant value in your program once it has been defined.
>> Myconstant = "Doesn't Change"
=> "Doesn't Change"
>> Myconstant = "Tried to Change"
(irb):85: warning: already initialized constant Myconstant
=> "Tried to Change"
Constants which are defined in a class or module are available to all objects and execution within the respective class or module. If you are outside of a module or class, in order to access a constant within that module or class, you must use two colons separating the class or module and the constant name.
You do not need to know the constants available in the module or class. This is because of the reflective attributes of Ruby, there is a class method, which is called constants, it returns as an array the constants available in the class or module it is called on.
=> ["PI", "E"]
Pretty pretty cool huh?