Question
Ruby kind_of? vs instance_of? vs is_a?: Differences and When to Use Them
Question
In Ruby, what is the difference between kind_of?, is_a?, and instance_of?? When should each one be used, and why does Ruby provide multiple methods for this kind of type check?
Short Answer
By the end of this page, you will understand how Ruby checks an object's type, how inheritance affects kind_of? and is_a?, why instance_of? is stricter, and which method is usually the best choice in everyday Ruby code.
Concept
Ruby objects belong to classes, and classes can inherit from other classes. Because of that, there is more than one way to ask what an object "is".
kind_of? and is_a? check whether an object is an instance of a class or any of its parent classes or included modules.
instance_of? checks whether an object is an instance of exactly one specific class.
Core idea
Suppose you have this inheritance chain:
class Animal
end
class Dog < Animal
end
A Dog object is:
- an instance of
Dog - also a kind of
Animal
That means:
dog = Dog.new
dog.instance_of?(Dog) # => true
dog.instance_of?(Animal) # => false
dog.kind_of?(Dog) # => true
dog.kind_of?(Animal) # => true
dog.is_a?(Dog) # => true
dog.is_a?(Animal)
Mental Model
Think of classes like family categories.
instance_of?asks: Who are your exact parents?kind_of?/is_a?ask: What family do you belong to?
If a Dog comes from the Dog class, then:
- it is exactly a
Dog - it also belongs to the larger
Animalfamily
So:
instance_of?(Dog)checks the exact label on the objectis_a?(Animal)checks the full family tree
A simple analogy:
instance_of?= exact job titleis_a?= department or profession category
Someone might be exactly a SeniorBackendEngineer, but they are also a kind of Engineer and a kind of Employee.
Syntax and Examples
Basic syntax
object.instance_of?(ClassName)
object.kind_of?(ClassName)
object.is_a?(ClassName)
Example with inheritance
class Vehicle
end
class Car < Vehicle
end
car = Car.new
puts car.instance_of?(Car) # true
puts car.instance_of?(Vehicle) # false
puts car.kind_of?(Car) # true
puts car.kind_of?(Vehicle) # true
puts car.is_a?(Car) # true
puts car.is_a?(Vehicle) # true
Explanation
instance_of?(Car)istruebecausecarwas created directly fromCar.instance_of?(Vehicle)is because was not created directly from .
Step by Step Execution
Consider this example:
class Animal
end
class Dog < Animal
end
dog = Dog.new
puts dog.instance_of?(Animal)
puts dog.is_a?(Animal)
Step by step
1. Define Animal
class Animal
end
This creates a class called Animal.
2. Define Dog < Animal
class Dog < Animal
end
This means Dog inherits from Animal.
3. Create a Dog object
Real World Use Cases
1. Accepting subclasses in APIs
If a method expects a general type, use is_a? or kind_of?.
def log_error(error)
raise ArgumentError, "Expected an exception" unless error.is_a?(Exception)
puts error.message
end
This allows custom exception subclasses too.
2. Validating objects by category
def print_total(value)
if value.is_a?(Numeric)
puts "$#{value}"
else
puts "Not a number"
end
end
This works for integers, floats, and other numeric types.
3. Exact class checks for strict logic
Sometimes you really need the object to be one exact class.
def raw_string_only(value)
unless value.instance_of?()
,
value
Real Codebase Usage
In real Ruby codebases, developers often avoid strict type checks unless they are truly necessary.
Common pattern: prefer flexible checks
def process_payment(payment)
raise ArgumentError, "Invalid payment" unless payment.is_a?(Payment)
payment.process
end
This allows subclasses such as CreditCardPayment or PaypalPayment.
Guard clauses
Type checks are often used in guard clauses near the top of a method.
def send_notification(user)
return unless user.is_a?(User)
# continue safely
end
Error handling
Ruby code often checks broad error categories rather than exact classes.
begin
risky_operation
rescue => e
if e.is_a?(StandardError)
puts e.message
end
Common Mistakes
1. Using instance_of? when subclasses should be allowed
Broken approach:
class Animal
end
class Dog < Animal
end
def handle_animal(animal)
unless animal.instance_of?(Animal)
raise ArgumentError, "Expected Animal"
end
end
handle_animal(Dog.new) # raises error
Problem:
Dogis a validAnimalsubtype, butinstance_of?rejects it.
Better:
def handle_animal(animal)
unless animal.is_a?(Animal)
raise ArgumentError, "Expected Animal"
end
Comparisons
| Method | Checks exact class? | Accepts parent classes? | Accepts included modules? | Typical use |
|---|---|---|---|---|
instance_of? | Yes | No | No | Strict exact-class matching |
kind_of? | No | Yes | Yes | General type/category check |
is_a? | No | Yes | Yes | Same as kind_of?, often more readable |
kind_of? vs is_a?
Cheat Sheet
obj.instance_of?(ClassName)
obj.kind_of?(ClassName)
obj.is_a?(ClassName)
Rules
-
instance_of?trueonly if the object's class is exactlyClassName- ignores parent classes
- ignores included modules
-
kind_of?trueif object is that class, a subclass, or includes that module
-
is_a?- same behavior as
kind_of? - usually chosen for readability
- same behavior as
Quick examples
class Animal; end
class Dog < Animal; end
dog = Dog.new
dog.instance_of?(Dog) # true
dog.instance_of?()
dog.is_a?()
dog.is_a?()
dog.kind_of?()
FAQ
What is the difference between kind_of? and is_a? in Ruby?
There is no behavioral difference. is_a? is an alias of kind_of?, so both return the same result.
When should I use instance_of? in Ruby?
Use it when you need to ensure an object belongs to one exact class and not any subclass.
Is is_a? usually better than instance_of??
In many cases, yes. is_a? works better with inheritance and is more flexible in object-oriented code.
Does is_a? work with modules?
Yes. If a class includes a module, is_a?(ThatModule) can return true.
Why does Ruby have both kind_of? and is_a??
Ruby often provides multiple method names for readability. Different names can make code read more naturally in different situations.
Should I avoid type checking in Ruby?
Not always, but many Ruby programs prefer duck typing. If you only need an object to support a method, may be a better fit.
Mini Project
Description
Build a small Ruby type-inspection script that compares instance_of?, kind_of?, and is_a? on custom classes and a module. This helps you see exactly how Ruby treats exact classes, parent classes, and included modules in practice.
Goal
Create a script that prints the result of several type checks and clearly shows the difference between strict and inheritance-aware checks.
Requirements
Requirement 1 Requirement 2 Requirement 3
Keep learning
Related questions
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Difference Between require and include in Ruby
Learn the difference between require and include in Ruby, when to load a file, and when to mix module methods into a class.
Fixing Ruby Gem Native Extension Errors: mkmf.rb Can't Find Header Files for Ruby
Learn why Ruby gem installs fail with missing ruby.h, how native extensions work, and how to fix header file errors on Linux servers.