Bird
0
0

Which method would you use to check if an object is an instance of a specific class or its subclass in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Variables and Data Types
Which method would you use to check if an object is an instance of a specific class or its subclass in Ruby?
A.class
B.is_a?
C.instance_of?
D.superclass
Step-by-Step Solution
Solution:
  1. Step 1: Understand .is_a? method

    .is_a? returns true if the object is an instance of the given class or its subclass.
  2. Step 2: Compare with other methods

    .instance_of? checks only exact class, .class returns class, .superclass returns superclass.
  3. Final Answer:

    .is_a? -> Option B
  4. Quick Check:

    .is_a? checks class or subclass [OK]
Quick Trick: Use .is_a? to check class or subclass membership [OK]
Common Mistakes:
MISTAKES
  • Using .class for subclass check
  • Confusing .instance_of? with .is_a?
  • Confusing with .superclass

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes