Bird
Raised Fist0
Rubyprogramming~10 mins

Subclass with < operator in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Subclass with < operator
Define Parent Class
↓
Define Subclass < Parent
↓
Override < operator method
↓
Create Subclass Instances
↓
Compare Instances with <
↓
Return true/false based on logic
This flow shows how to create a subclass that overrides the < operator to compare instances based on custom logic.
Execution Sample
Ruby
class Animal
  attr_reader :age
  def initialize(age)
    @age = age
  end
end

class Dog < Animal
  def <(other)
    age < other.age
  end
end

puts Dog.new(3) < Dog.new(5)
This code defines a subclass Dog that inherits from Animal and overrides the < operator to compare dogs by age.
Execution Table
StepActionEvaluationResult
1Create Dog instance with age 3Dog.new(3)Dog object with age=3
2Create Dog instance with age 5Dog.new(5)Dog object with age=5
3Compare Dog(3) < Dog(5)3 < 5true
4Print resultputs truetrue printed to console
5End of programNo more codeProgram stops
💡 Program ends after printing the comparison result.
Variable Tracker
VariableStartAfter 1After 2Final
dog1.agenil333
dog2.agenilnil55
comparison_resultnilniltruetrue
Key Moments - 2 Insights
Why do we override the < method in the subclass?
We override < in the subclass to define how two subclass objects should be compared, as shown in execution_table row 3 where Dog(3) < Dog(5) uses the custom logic.
What does the < operator compare inside the method?
Inside the < method, it compares the age attributes of the two Dog instances, as seen in execution_table row 3 where 3 < 5 is evaluated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of Dog.new(3) < Dog.new(5)?
Anil
Bfalse
Ctrue
DError
💡 Hint
Check execution_table row 3 where the comparison 3 < 5 evaluates to true.
At which step is the Dog instance with age 5 created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 2 where Dog.new(5) is created.
If we change dog1's age to 6, what would Dog.new(6) < Dog.new(5) return?
Afalse
Btrue
Cnil
DError
💡 Hint
Refer to variable_tracker and execution_table row 3 logic: 6 < 5 is false.
Concept Snapshot
Subclass with < operator in Ruby:
- Define subclass with < ParentClass
- Override < method to compare attributes
- Use instance variables for comparison
- Return true/false based on logic
- Enables custom object comparisons
Full Transcript
This example shows how to create a subclass in Ruby that overrides the less-than operator (<). We define a parent class Animal with an age attribute. Then we create a subclass Dog that inherits from Animal. Inside Dog, we override the < method to compare the age of two Dog instances. When we create two Dog objects with ages 3 and 5, comparing them with < calls our custom method, which returns true because 3 is less than 5. The program prints true and then ends.

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

  1. 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.
  2. Step 2: Apply to ChildClass < ParentClass

    This expression returns true if ChildClass inherits from ParentClass, meaning ChildClass is a subclass.
  3. Final Answer:

    If ChildClass is a subclass of ParentClass -> Option C
  4. 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

  1. Step 1: Recall Ruby subclass syntax

    Ruby uses the < operator between classes to check subclass relationships, like Dog < Animal.
  2. 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.
  3. Final Answer:

    Dog < Animal -> Option A
  4. 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

  1. 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.
  2. Step 2: Determine output of puts statements

    The first puts prints true, the second prints false, each on its own line.
  3. Final Answer:

    true false -> Option B
  4. Quick Check:

    Subclass true, unrelated false [OK]
Hint: Subclass < Parent = true; unrelated classes = false [OK]
Common Mistakes:
  • Assuming unrelated classes return true
  • Confusing instance and class comparisons
  • Mixing up order of classes in < operator
4. Consider this Ruby code snippet:
class Fruit; end
class Apple < Fruit; end

puts Apple < fruit

What is the error and how to fix it?
medium
A. No error; output is true
B. SyntaxError: invalid syntax; fix by adding parentheses
C. TypeError: wrong argument type; fix by using instance instead of class
D. NameError: uninitialized constant 'fruit'; fix by capitalizing 'Fruit'

Solution

  1. 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.
  2. Step 2: Fix the error

    Change 'fruit' to 'Fruit' to reference the class correctly.
  3. Final Answer:

    NameError: uninitialized constant 'fruit'; fix by capitalizing 'Fruit' -> Option D
  4. Quick Check:

    Class names start uppercase [OK]
Hint: Class names always start with uppercase letter [OK]
Common Mistakes:
  • Using lowercase for class names
  • Confusing variables with class constants
  • Trying to call < on instances instead of classes
5. You have these Ruby 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?
hard
A. true false false
B. true true false
C. false false true
D. true false true

Solution

  1. Step 1: Evaluate Dog < Animal

    Dog inherits from Mammal, which inherits from Animal, so Dog is a subclass of Animal. Returns true.
  2. Step 2: Evaluate Fish < Mammal

    Fish inherits directly from Animal, not Mammal. So Fish is not a subclass of Mammal. Returns false.
  3. Step 3: Evaluate Mammal < Dog

    Mammal is a superclass of Dog, not a subclass. So this returns false.
  4. Final Answer:

    true false false -> Option A
  5. Quick Check:

    Subclass chain true, unrelated or reversed false [OK]
Hint: Subclass < superclass = true; reversed or unrelated = false [OK]
Common Mistakes:
  • Assuming superclass < subclass returns true
  • Ignoring multi-level inheritance
  • Mixing up class hierarchy direction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard