A constant in a Ruby class is a value that is meant to stay the same throughout the program. It is written with a name starting with a capital letter and usually holds data that should not change.
Click to reveal answer
beginner
How do you define a constant inside a Ruby class?
You define a constant inside a Ruby class by writing the constant name starting with a capital letter, followed by an equal sign and the value. For example: <pre>class MyClass
MY_CONSTANT = 10
end</pre>
Click to reveal answer
intermediate
Can you change the value of a constant in Ruby after it is set?
Technically, you can change a constant's value in Ruby, but it will give a warning because constants are meant to stay the same. Changing them is not recommended.
Click to reveal answer
beginner
How do you access a constant defined inside a Ruby class from outside the class?
You access a constant from outside the class by using the scope resolution operator :: with the class name. For example: <pre>MyClass::MY_CONSTANT</pre>
Click to reveal answer
intermediate
What happens if you define a constant with the same name in a subclass in Ruby?
If a subclass defines a constant with the same name as in its parent class, the subclass's constant will override the parent's constant when accessed from the subclass.
Click to reveal answer
How do you write a constant name in Ruby?
AStarts with a lowercase letter
BStarts with a number
CStarts with an underscore
DStarts with a capital letter
✗ Incorrect
In Ruby, constants must start with a capital letter.
How do you access a constant MY_CONST inside class MyClass from outside?
AMyClass::MY_CONST
BMyClass.MY_CONST
CMyClass->MY_CONST
DMY_CONST.MyClass
✗ Incorrect
Use the scope resolution operator :: to access constants inside classes.
What happens if you change a constant's value in Ruby?
ARuby deletes the constant
BRuby throws an error and stops
CRuby allows it but shows a warning
DRuby ignores the change silently
✗ Incorrect
Ruby allows changing constants but warns you because it's not recommended.
Where should constants be defined in Ruby?
AOnly inside methods
BInside classes or modules
COnly in global scope
DInside local variables
✗ Incorrect
Constants are usually defined inside classes or modules to organize them.
If a subclass defines a constant with the same name as its parent, which one is used inside the subclass?
AThe subclass's constant
BThe parent's constant
CBoth constants are merged
DAn error occurs
✗ Incorrect
The subclass's constant overrides the parent's constant when accessed from the subclass.
Explain how to define and access constants inside Ruby classes.
Think about how you write constants and how you get their values from outside.
You got /4 concepts.
Describe what happens when you try to change a constant's value in Ruby and why it matters.
Consider Ruby's behavior and why constants exist.
You got /3 concepts.
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