Subclass with < operator in Ruby - Time & Space Complexity
When we define the < operator in a class, it often compares objects. Understanding how long these comparisons take helps us know how the program grows with more data.
We want to find out how the time needed changes as we compare more objects.
Analyze the time complexity of the following code snippet.
class Box
include Comparable
attr_reader :volume
def initialize(volume)
@volume = volume
end
def <(other)
volume < other.volume
end
end
boxes = [Box.new(10), Box.new(20), Box.new(15)]
boxes.sort!
This code defines a Box class with a < operator to compare volumes, then sorts an array of boxes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing two Box objects using the < operator during sorting.
- How many times: The sort method compares pairs multiple times, depending on the sorting algorithm.
As the number of boxes grows, the number of comparisons grows faster than the number of boxes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 comparisons |
| 100 | About 500 comparisons |
| 1000 | About 15,000 comparisons |
Pattern observation: When input size grows, comparisons grow much faster, roughly like n × log n.
Time Complexity: O(n log n)
This means if you double the number of boxes, the number of comparisons roughly doubles (not triples).
[X] Wrong: "Sorting with a custom < operator always takes linear time because it just compares once per item."
[OK] Correct: Sorting needs many comparisons between pairs, not just one per item, so it takes more time as the list grows.
Understanding how custom comparison affects sorting time helps you explain performance in real programs. It shows you can think about how your code behaves with more data, a key skill in programming.
"What if the < operator compared multiple attributes instead of just one? How would the time complexity change?"
Practice
ChildClass < ParentClass check?Solution
Step 1: Understand the < operator for classes
In Ruby, the < operator between classes checks inheritance, specifically if the left class is a subclass of the right class.Step 2: Apply to ChildClass < ParentClass
This expression returns true if ChildClass inherits from ParentClass, meaning ChildClass is a subclass.Final Answer:
If ChildClass is a subclass of ParentClass -> Option CQuick Check:
Subclass check = true if inheritance [OK]
- Confusing subclass check with instance check
- Thinking < checks equality of classes
- Reversing the order of classes in the expression
Dog is a subclass of Animal in Ruby?Solution
Step 1: Recall Ruby subclass syntax
Ruby uses the < operator between classes to check subclass relationships, likeDog < Animal.Step 2: Evaluate options
Methods likeis_subclass_of?andsubclass_of?do not exist on classes.Dog < Animal()has incorrect syntax with parentheses. OnlyDog < Animalis valid Ruby syntax.Final Answer:
Dog < Animal -> Option AQuick Check:
Correct syntax uses < operator [OK]
- Using methods that don't exist for subclass check
- Adding parentheses incorrectly
- Using instance methods instead of class operators
class Vehicle; end class Car < Vehicle; end class Bike; end puts Car < Vehicle puts Bike < Vehicle
Solution
Step 1: Analyze class inheritance
Car inherits from Vehicle, soCar < Vehiclereturns true. Bike does not inherit from Vehicle, soBike < Vehiclereturns false.Step 2: Determine output of puts statements
The first puts prints true, the second prints false, each on its own line.Final Answer:
true false -> Option BQuick Check:
Subclass true, unrelated false [OK]
- Assuming unrelated classes return true
- Confusing instance and class comparisons
- Mixing up order of classes in < operator
class Fruit; end class Apple < Fruit; end puts Apple < fruit
What is the error and how to fix it?
Solution
Step 1: Identify the error
The code uses lowercase 'fruit' instead of 'Fruit'. Ruby constants (classes) must start with uppercase letters. This causes a NameError.Step 2: Fix the error
Change 'fruit' to 'Fruit' to reference the class correctly.Final Answer:
NameError: uninitialized constant 'fruit'; fix by capitalizing 'Fruit' -> Option DQuick Check:
Class names start uppercase [OK]
- Using lowercase for class names
- Confusing variables with class constants
- Trying to call < on instances instead of classes
class Animal; end class Mammal < Animal; end class Dog < Mammal; end class Fish < Animal; end puts Dog < Animal puts Fish < Mammal puts Mammal < Dog
What is the output and why?
Solution
Step 1: Evaluate Dog < Animal
Dog inherits from Mammal, which inherits from Animal, so Dog is a subclass of Animal. Returns true.Step 2: Evaluate Fish < Mammal
Fish inherits directly from Animal, not Mammal. So Fish is not a subclass of Mammal. Returns false.Step 3: Evaluate Mammal < Dog
Mammal is a superclass of Dog, not a subclass. So this returns false.Final Answer:
true false false -> Option AQuick Check:
Subclass chain true, unrelated or reversed false [OK]
- Assuming superclass < subclass returns true
- Ignoring multi-level inheritance
- Mixing up class hierarchy direction
Ready to prove it?
Pass the test and this pattern moves unknown
