Constants (should) have the same value during execution of the program.
Its like a variable, except a Ruby constant should not change its value.
This is very useful for certain types of data that never change. Think of a value like Pi or the version of the program.
If you try to change the value of a constant, the Ruby interpreter will throw an warning. Ruby is however not strict, it will change the value anyway.
You can create a constant by adding _CONST to the name. In Ruby, the value of a constant can be changed but it will show a warning.
This wil show the warning:
test.rb:3: warning: already initialized constant A_CONST
But the actual value is changed. In other programming languages constants are more strict, its value cannot be changed.
Unless a value is actually assigned to them, constants do not exist.
Constants cannot be defined inside methods. But they can be defined throughout the program or inside classes.
To access constants inside classes, you can use the :: operator.
The simple example below deomnstrates how to use a constant defined in a class.
This will output the value of the constant.