Constants
Define a constant
Section titled “Define a constant”MY_CONSTANT = "Hello, world" # constantConstant = 'This is also constant' # constantmy_variable = "Hello, venus" # not constatnConstant name start with capital letter. Everything that start with capital letter are considered as constant in Ruby. So class and module are also constant.
Best practice is use all capital letter for declaring constant.
Modify a Constant
Section titled “Modify a Constant”MY_CONSTANT = "Hello, world"MY_CONSTANT = "Hullo, world"The above code results in a warning, because you should be using variables if you want to change their values. However it is possible to change one letter at a time in a constant without a warning, like this:
MY_CONSTANT = "Hello, world"MY_CONSTANT[1] = "u"Now, after changing the second letter of MY_CONSTANT, it becomes "Hullo, world".
Constants cannot be defined in methods
Section titled “Constants cannot be defined in methods”def say_hi MESSAGE = "Hello" puts MESSAGEendThe above code results in an error: SyntaxError: (irb):2: dynamic constant assignment.
Define and change constants in a class
Section titled “Define and change constants in a class”class Message DEFAULT_MESSAGE = "Hello, world"
def speak(message = nil) if message puts message else puts DEFAULT_MESSAGE end endendThe constant DEFAULT_MESSAGE can be changed with the following code:
Message::DEFAULT_MESSAGE = "Hullo, world"Syntax
Section titled “Syntax”- MY_CONSTANT_NAME = “my value”
Remarks
Section titled “Remarks”Constants are useful in Ruby when you have values that you do not want to be mistakenly changed in a program, such as API keys.