Constants hold values that do not change. In classes, they keep important fixed information easy to find and use.
When you want to store a fixed value like a tax rate used by all objects of a class.
When you need a label or name that stays the same for every instance.
When you want to avoid repeating the same value in many places in your code.
When you want to make your code easier to read by giving a meaningful name to a fixed value.
Syntax
Ruby
class ClassName
CONSTANT_NAME = value
end
Constants start with a capital letter.
They are written inside the class but outside any method.
Examples
This sets a constant WHEELS to 4 inside the Car class.
Ruby
class Car
WHEELS = 4end
PI is a constant for the value of pi inside the Circle class.
Ruby
class Circle
PI = 3.14end
ROLE is a constant string inside the User class.
Ruby
class User
ROLE = 'guest'end
Sample Program
This program defines a constant GENRE inside the Book class. The method show_genre prints the constant value.
Ruby
class Book
GENRE = 'Fiction'def show_genre
puts"Genre: #{GENRE}"endend
book = Book.new
book.show_genre
OutputSuccess
Important Notes
Constants can be accessed inside the class directly by their name.
You can also access constants outside the class using ClassName::CONSTANT_NAME.
Ruby warns if you try to change a constant, but it still allows it.
Summary
Constants store fixed values inside classes.
They start with a capital letter and are written outside methods.
Use them to keep important values easy to find and use.
Practice
(1/5)
1. What is true about constants in Ruby classes?
easy
A. Constants start with a lowercase letter.
B. Constants can be changed anytime like variables.
C. Constants start with a capital letter and are defined outside methods.
D. Constants must be defined inside methods only.
Solution
Step 1: Understand constant naming rules
In Ruby, constants always start with a capital letter and are usually defined outside methods within classes or modules.
Step 2: Check where constants are defined
Constants are not meant to be changed and are not defined inside methods but at the class/module level.
Final Answer:
Constants start with a capital letter and are defined outside methods. -> Option C
Quick Check:
Constants = Capital letter + outside methods [OK]
Hint: Constants start uppercase and live outside methods [OK]
Common Mistakes:
Thinking constants can be changed like variables
Defining constants inside methods
Using lowercase letters for constants
2. Which of the following is the correct way to define a constant inside a Ruby class?
easy
A. class MyClass
def PI = 3.14
end
B. class MyClass
PI = 3.14
end
C. class MyClass
pi = 3.14
end
D. class MyClass
@PI = 3.14
end
Solution
Step 1: Identify constant syntax
Constants in Ruby start with a capital letter and are assigned directly inside the class but outside any method.
Step 2: Check each option
class MyClass
PI = 3.14
end correctly assigns PI = 3.14 inside the class. class MyClass
pi = 3.14
end uses lowercase (not constant). class MyClass
def PI = 3.14
end tries to define a method named PI incorrectly. class MyClass
@PI = 3.14
end uses an instance variable (@PI), not a constant.
Final Answer:
class MyClass
PI = 3.14
end -> Option B
Quick Check:
Constants = Capital letter + direct assignment [OK]
Hint: Constants = Capital letter + direct assignment inside class [OK]
Common Mistakes:
Using lowercase names for constants
Defining constants inside methods
Confusing constants with instance variables
3. What will be the output of this Ruby code?
class Car
WHEELS = 4
def wheels
WHEELS
end
end
car = Car.new
puts car.wheels