Constants in classes in Ruby - Time & Space Complexity
Let's see how accessing constants inside classes affects the time it takes for a program to run.
We want to know how the time changes when we get constant values from classes.
Analyze the time complexity of the following code snippet.
class MyClass
CONST_VALUE = 42
def get_constant
CONST_VALUE
end
end
obj = MyClass.new
10.times { obj.get_constant }
This code defines a class with a constant and a method that returns it. Then it calls that method multiple times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the method that returns the constant.
- How many times: The method is called 10 times in a loop.
Getting a constant value from a class is very fast and does not depend on input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 simple constant lookups |
| 100 | 100 simple constant lookups |
| 1000 | 1000 simple constant lookups |
Pattern observation: The time grows directly with the number of times you ask for the constant, but each lookup is very quick and does not get slower with bigger input.
Time Complexity: O(n)
This means the total time grows linearly with how many times you access the constant, but each access is very fast and simple.
[X] Wrong: "Accessing a constant inside a class takes constant time no matter how many times you do it, so the loop doesn't add time."
[OK] Correct: Each access is fast, but if you do it many times, the total time adds up linearly with the number of accesses.
Understanding how constants behave in classes helps you explain how simple data access works in real programs, a useful skill for many coding tasks.
"What if the constant was a large array and the method returned a copy each time? How would the time complexity change?"
Practice
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 CQuick Check:
Constants = Capital letter + outside methods [OK]
- Thinking constants can be changed like variables
- Defining constants inside methods
- Using lowercase letters for constants
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 BQuick Check:
Constants = Capital letter + direct assignment [OK]
- Using lowercase names for constants
- Defining constants inside methods
- Confusing constants with instance variables
class Car
WHEELS = 4
def wheels
WHEELS
end
end
car = Car.new
puts car.wheelsSolution
Step 1: Understand constant access inside instance method
The constant WHEELS is defined inside the class Car. Inside instance methods, constants can be accessed directly by their name.Step 2: Trace the method call
Calling car.wheels returns the constant WHEELS value, which is 4. Then puts prints 4.Final Answer:
4 -> Option AQuick Check:
Constant accessed inside method = 4 [OK]
- Expecting an error for constant access
- Thinking constant name prints instead of value
- Confusing constants with instance variables
class Book
TITLE = "Ruby Guide"
def change_title
TITLE = "New Title"
end
endSolution
Step 1: Check constant assignment rules
Constants in Ruby should not be reassigned once set. Assigning a constant inside a method is not allowed and causes a warning or error.Step 2: Analyze the code
The method change_title tries to assign a new value to TITLE constant, which is invalid.Final Answer:
Cannot assign to constant TITLE inside method. -> Option DQuick Check:
Constants cannot be reassigned inside methods [OK]
- Trying to change constants inside methods
- Confusing constants with variables
- Ignoring Ruby warnings about constant reassignment
puts Vehicle::TYPE print?
class Vehicle
TYPE = "Transport"
class Car
TYPE = "Car"
end
end
puts Vehicle::TYPESolution
Step 1: Understand constant lookup in nested classes
Constants are scoped to the class they are defined in. Vehicle::TYPE refers to the constant TYPE inside Vehicle, not inside Vehicle::Car.Step 2: Check the code output
Vehicle::TYPE is "Transport". The inner class Car has its own TYPE, but it is not accessed here.Final Answer:
"Transport" -> Option AQuick Check:
Outer class constant accessed by Vehicle::TYPE = "Transport" [OK]
- Confusing inner class constant with outer class constant
- Expecting error for nested constants
- Using wrong scope operator
Ready to prove it?
Pass the test and this pattern moves unknown
