What does the < operator mean when used between two classes in Ruby?
The < operator checks if the left class is a subclass of the right class. It returns true if the left class inherits from the right class, otherwise nil.
Click to reveal answer
beginner
How can you check if class B is a subclass of class A in Ruby?
You can write B < A. If B inherits from A, this expression returns true.
Click to reveal answer
beginner
Given <br><code>class Animal; end<br>class Dog < Animal; end</code><br>What is the result of <code>Dog < Animal</code>?
It returns true because Dog is a subclass of Animal.
Click to reveal answer
beginner
What will Animal < Dog return given the previous classes?
It returns nil because Animal is not a subclass of Dog; it is the other way around.
Click to reveal answer
intermediate
Can the < operator be used to check subclass relationships between unrelated classes?
No. If the classes are unrelated, the < operator returns nil, meaning no subclass relationship exists.
Click to reveal answer
What does Child < Parent return if Child inherits from Parent?
ARaises an error
Bfalse
Cnil
Dtrue
✗ Incorrect
If Child inherits from Parent, Child < Parent returns true.
What does Parent < Child return if Child inherits from Parent?
Atrue
Bnil
Cfalse
DRaises an error
✗ Incorrect
Parent is not a subclass of Child, so Parent < Child returns nil.
What is the result of String < Numeric in Ruby?
Anil
Btrue
Cfalse
DRaises an error
✗ Incorrect
String and Numeric are unrelated classes, so the < operator returns nil.
Which operator checks subclass relationships between classes in Ruby?
A==
B>
C<
D!=
✗ Incorrect
The < operator checks if one class is a subclass of another.
If class Cat < Animal; end, what does Cat < Animal return?
Atrue
BRaises an error
Cnil
Dfalse
✗ Incorrect
Cat inherits from Animal, so Cat < Animal returns true.
Explain how the < operator works to check subclass relationships in Ruby.
Think about inheritance and how Ruby checks if one class inherits from another.
You got /3 concepts.
Describe what happens when you compare two unrelated classes using the < operator in Ruby.
Consider what Ruby returns when no inheritance link exists.
You got /3 concepts.
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.