📖 Scenario: Imagine you are creating a simple program to manage information about cars. Each car has a brand and a fixed number of wheels. The number of wheels is always the same for all cars, so it makes sense to store it as a constant inside the class.
🎯 Goal: You will create a class called Car with a constant WHEELS set to 4. Then you will create a method to display the number of wheels and the brand of the car.
📋 What You'll Learn
Create a class called Car
Inside the class, define a constant WHEELS with the value 4
Add an initialize method that takes a brand parameter and saves it
Add a method display_info that prints the brand and the number of wheels
Create an instance of Car with brand "Toyota"
Call the display_info method on the instance
💡 Why This Matters
🌍 Real World
Constants in classes are useful to store values that do not change, like fixed settings or properties shared by all objects of that class.
💼 Career
Understanding constants in classes helps you write clear and maintainable code, which is important in software development jobs.
Progress0 / 4 steps
1
Create the Car class with a brand attribute
Create a class called Car with an initialize method that takes a parameter brand and saves it in an instance variable @brand.
Ruby
Hint
Remember to use def initialize(brand) and assign @brand = brand.
2
Add a constant WHEELS with value 4
Inside the Car class, add a constant called WHEELS and set it to 4.
Ruby
Hint
Constants in Ruby start with a capital letter. Write WHEELS = 4 inside the class.
3
Add a method to display brand and wheels
Add a method called display_info inside the Car class that prints the brand and the number of wheels using puts. Use @brand for the brand and Car::WHEELS to access the constant.
Ruby
Hint
Use def display_info and inside it write puts "Brand: #{@brand}, Wheels: #{Car::WHEELS}".
4
Create a Car instance and call display_info
Create a variable my_car and assign it a new Car object with brand "Toyota". Then call my_car.display_info to print the information.
Ruby
Hint
Create my_car = Car.new("Toyota") and then call my_car.display_info.
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