📖 Scenario: Imagine you are creating a simple system to compare different types of boxes by their volume. You want to be able to say if one box is smaller than another using the < operator.
🎯 Goal: You will create a subclass called Box that inherits from a class Container. You will add the < operator to compare boxes by their volume.
📋 What You'll Learn
Create a class called Container with attributes length, width, and height.
Create a subclass called Box that inherits from Container.
Add a method <(other) in Box to compare volumes of two boxes.
Create two Box objects with given dimensions.
Print the result of comparing the two boxes using the < operator.
💡 Why This Matters
🌍 Real World
Comparing sizes of physical objects like boxes is common in shipping and storage to optimize space.
💼 Career
Understanding subclassing and operator overloading is useful for designing clear and reusable code in software development.
Progress0 / 4 steps
1
Create the Container class with dimensions
Create a class called Container with an initialize method that takes length, width, and height as parameters and sets them as instance variables.
Ruby
Hint
Use def initialize(length, width, height) and set instance variables with @length = length, etc.
2
Create the Box subclass inheriting from Container
Create a subclass called Box that inherits from Container. Add an initialize method in Box that calls super with length, width, and height.
Ruby
Hint
Use class Box < Container and call super(length, width, height) inside initialize.
3
Add the < operator method to compare volumes
In the Box class, add a method def <(other) that returns true if the volume of the current box is less than the volume of other. Calculate volume as @length * @width * @height.
Ruby
Hint
Use def <(other) and compare volumes using instance variables. Use other.instance_variable_get(:@length) to access other's dimensions.
4
Create two Box objects and print comparison result
Create two Box objects called box1 with dimensions 2, 3, 4 and box2 with dimensions 3, 3, 3. Then print the result of box1 < box2.
Ruby
Hint
Create box1 and box2 with Box.new and print box1 < box2.
Practice
(1/5)
1. In Ruby, what does the expression ChildClass < ParentClass check?
easy
A. If ChildClass and ParentClass are the same class
B. If ChildClass is an instance of ParentClass
C. If ChildClass is a subclass of ParentClass
D. If ParentClass is a subclass of ChildClass
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 C
Quick Check:
Subclass check = true if inheritance [OK]
Hint: Remember: < means 'is subclass of' in Ruby classes [OK]
Common Mistakes:
Confusing subclass check with instance check
Thinking < checks equality of classes
Reversing the order of classes in the expression
2. Which of the following is the correct syntax to check if Dog is a subclass of Animal in Ruby?
easy
A. Dog < Animal
B. Dog.is_subclass_of?(Animal)
C. Dog.subclass_of? Animal
D. Dog < Animal()
Solution
Step 1: Recall Ruby subclass syntax
Ruby uses the < operator between classes to check subclass relationships, like Dog < Animal.
Step 2: Evaluate options
Methods like is_subclass_of? and subclass_of? do not exist on classes. Dog < Animal() has incorrect syntax with parentheses. Only Dog < Animal is valid Ruby syntax.
Final Answer:
Dog < Animal -> Option A
Quick Check:
Correct syntax uses < operator [OK]
Hint: Use < directly between classes, no method needed [OK]
Common Mistakes:
Using methods that don't exist for subclass check
Adding parentheses incorrectly
Using instance methods instead of class operators
3. What is the output of this Ruby code?
class Vehicle; end
class Car < Vehicle; end
class Bike; end
puts Car < Vehicle
puts Bike < Vehicle
medium
A. false
false
B. true
false
C. true
true
D. false
true
Solution
Step 1: Analyze class inheritance
Car inherits from Vehicle, so Car < Vehicle returns true. Bike does not inherit from Vehicle, so Bike < Vehicle returns false.
Step 2: Determine output of puts statements
The first puts prints true, the second prints false, each on its own line.