We use is_a? and kind_of? to check what type an object is. This helps us decide what actions to take based on the object's type.
Is_a? and kind_of? for type checking in Ruby
object.is_a?(ClassName) object.kind_of?(ClassName)
Both is_a? and kind_of? do the same thing in Ruby.
They return true if the object is an instance of the class or its subclass, otherwise false.
5.is_a?(Integer) # returns true "hello".is_a?(String) # returns true 5.is_a?(String) # returns false
class Animal; end class Dog < Animal; end dog = Dog.new dog.kind_of?(Animal) # returns true dog.kind_of?(Dog) # returns true dog.kind_of?(String) # returns false
This program creates a class Vehicle and a subclass Car. It then checks if my_car is a Car, a Vehicle, or a String.
class Vehicle end class Car < Vehicle end my_car = Car.new puts my_car.is_a?(Car) # true puts my_car.is_a?(Vehicle) # true puts my_car.is_a?(String) # false
is_a? and kind_of? are interchangeable; use whichever you prefer.
They check inheritance, so subclasses return true for their parent classes.
Use these methods to write safer code that adapts to different object types.
is_a? and kind_of? check an object's type or its parent types.
They return true if the object is an instance of the class or subclass.
Use them to make decisions based on object types and avoid errors.
Practice
is_a? check when called on an object?Solution
Step 1: Understand the purpose of
The methodis_a?is_a?checks if an object is an instance of a specific class or any of its subclasses.Step 2: Compare with other options
Options A, C, and D describe different checks unrelated tois_a?.Final Answer:
If the object is an instance of a given class or its subclass -> Option CQuick Check:
is_a?checks instance or subclass [OK]
- Confusing is_a? with equality check
- Thinking is_a? checks method presence
- Assuming is_a? only checks exact class
obj is a kind of Array in Ruby?Solution
Step 1: Recall method syntax
Bothis_a?andkind_of?require a question mark and parentheses around the class name.Step 2: Check each option
obj.kind_of?(Array) uses correct method name and syntax. Options B and C miss the question mark, and D has incorrect bracket usage.Final Answer:
obj.kind_of?(Array) -> Option BQuick Check:
Correct method syntax includes ? and parentheses [OK]
- Omitting the question mark in method name
- Using square brackets instead of parentheses
- Confusing is_a and kind_of syntax
class Animal; end class Dog < Animal; end dog = Dog.new puts dog.is_a?(Animal) puts dog.kind_of?(Dog) puts dog.is_a?(String)
Solution
Step 1: Understand class inheritance
Dog inherits from Animal, so a Dog object is both a Dog and an Animal instance.Step 2: Evaluate each method call
dog.is_a?(Animal)returns true because Dog is subclass of Animal.dog.kind_of?(Dog)returns true because dog is instance of Dog.dog.is_a?(String)returns false because dog is not a String.Final Answer:
true true false -> Option AQuick Check:
Inheritance means is_a? true for parent classes [OK]
- Assuming is_a? false for parent classes
- Mixing up kind_of? and is_a? results
- Thinking dog is a String
num = 10 if num.is_a?(Integer) puts "Number is integer" else puts "Number is not integer" end
Solution
Step 1: Check method usage
The methodis_a?is correctly called with parentheses and a valid class Integer.Step 2: Verify code logic and syntax
The if condition is valid and will print "Number is integer" because 10 is an Integer.Final Answer:
No error; code runs and prints 'Number is integer' -> Option DQuick Check:
Correct syntax and logic means no error [OK]
- Omitting parentheses in method call
- Confusing is_a? with kind_of? as error
- Assuming is_a? is undefined for numbers
String or a Numeric (including Integer and Float). Which Ruby code correctly uses is_a? or kind_of? to check this?Solution
Step 1: Understand class hierarchy
Numeric is a superclass of Integer and Float, so checkingis_a?(Numeric)covers all number types.Step 2: Analyze each option
if obj.is_a?(String) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end correctly checks for String and then Numeric.
if obj.kind_of?(String) puts "It's a string" elsif obj.kind_of?(Integer) puts "It's a number" else puts "Unknown type" end checks Integer only, missing Float.
if obj.is_a?(String) puts "It's a string" elsif obj.kind_of?(Float) puts "It's a number" else puts "Unknown type" end checks only Float, missing Integer.
if obj.is_a?(Object) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end checks Object first, which is always true, so logic fails.Final Answer:
Option A code correctly distinguishes String and all Numeric types -> Option AQuick Check:
Use Numeric superclass to cover all numbers [OK]
- Checking only Integer or Float separately
- Using Object which is always true
- Mixing is_a? and kind_of? incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
