Question
In Python, you can check a value’s type like this:
a = 1
type(a)
# <type 'int'>
In Ruby, I know I can write:
1.class
# => Integer
Is using .class the correct way to determine the type of an object in Ruby? If so, when should it be used, and are there better Ruby-style alternatives in some situations?
Short Answer
By the end of this page, you will understand how Ruby identifies object types, when .class is useful, and why methods like is_a? and kind_of? are often better choices in real programs. You will also learn common pitfalls around exact class checks versus inheritance-aware checks.
Concept
Ruby is a fully object-oriented language, which means everything is an object. Numbers, strings, arrays, hashes, and even classes themselves are objects.
When you call:
1.class
Ruby returns the object's class:
Integer
So yes, .class is a valid and common way to inspect what class an object belongs to.
However, in Ruby, asking for an object's exact class is not always the best way to answer the real question. Often, what you actually want to know is:
- Can this object be used like a string?
- Is this object a kind of number?
- Does this object behave like an array?
That matters because Ruby supports inheritance. An object may belong to a subclass, but still be usable anywhere its parent class is expected.
For that reason, Ruby programmers often prefer:
obj.is_a?(SomeClass)
or
obj.kind_of?(SomeClass)
These check whether the object is an instance of a class or any subclass of it.
Why this matters
If you compare exact classes using .class == SomeClass, your code can become too strict. It may reject objects that should work perfectly well.
Mental Model
Think of .class like checking someone's official job title.
.classasks: "What is your exact title?"is_a?asks: "Are you part of this broader category of workers?"
For example:
- A
Dogmight have the exact classDog - But it is also an
Animal
So:
dog.class == Dog # exact title
dog.is_a?(Animal) # broader category
In real programs, the broader category is often more useful than the exact title.
Syntax and Examples
Core syntax
obj.class
obj.is_a?(SomeClass)
obj.kind_of?(SomeClass)
Example 1: Exact class
value = 1
puts value.class
# Integer
This returns the exact class of value.
Example 2: Check whether an object belongs to a type hierarchy
value = 1
puts value.is_a?(Integer)
# true
puts value.is_a?(Numeric)
# true
puts value.is_a?(Object)
# true
This is often more useful because Integer inherits from Numeric, and all regular Ruby objects inherit from Object.
Example 3: Inheritance example
class Animal
end
class Dog <
dog = .new
puts dog.
puts dog. ==
puts dog.is_a?()
Step by Step Execution
Consider this Ruby code:
class Animal
end
class Cat < Animal
end
pet = Cat.new
puts pet.class
puts pet.is_a?(Cat)
puts pet.is_a?(Animal)
puts pet.class == Animal
Step by step
- Ruby defines a class named
Animal. - Ruby defines a class named
Catthat inherits fromAnimal. pet = Cat.newcreates a new object whose exact class isCat.pet.classreturnsCat, because that is the object's exact class.pet.is_a?(Cat)returnstrue, becausepetis aCat.pet.is_a?(Animal)also returns , because inherits from .
Real World Use Cases
Debugging values
When debugging, .class is very useful:
puts params[:id].class
This helps you see whether you received a String, Integer, Array, or something unexpected.
Input validation
You may want to ensure a method receives a number-like value:
def square(n)
raise ArgumentError, "Must be numeric" unless n.is_a?(Numeric)
n * n
end
Working with collections
You might handle arrays differently from single values:
if data.is_a?(Array)
data.each { |item| puts item }
end
API and JSON processing
Parsed data may contain hashes, arrays, strings, booleans, and numbers. Type checks can help decide how to process each value.
Real Codebase Usage
In real Ruby codebases, developers usually combine type inspection with Ruby-style flexibility.
Common patterns
Guard clauses
def process(items)
return [] unless items.is_a?(Array)
items.map(&:to_s)
end
This exits early if the input is not usable.
Validation
def set_age(age)
raise ArgumentError, "age must be an Integer" unless age.is_a?(Integer)
@age = age
end
Accepting broader categories
def total(number)
raise ArgumentError unless number.is_a?(Numeric)
number + 10
end
Using Numeric is often better than only allowing , because it also supports , , and others.
Common Mistakes
1. Using .class == SomeClass when inheritance matters
Broken approach:
def animal?(obj)
obj.class == Animal
end
This returns false for subclasses like Dog < Animal.
Better:
def animal?(obj)
obj.is_a?(Animal)
end
2. Checking exact types too early
Beginners sometimes write code that only accepts one exact class even when several related classes should work.
Too strict:
def add_tax(amount)
raise "bad type" unless amount.class == Integer
amount * 1.2
end
Better:
Comparisons
| Approach | What it checks | Inheritance-aware? | Best use |
|---|---|---|---|
obj.class | Exact class object | No | Debugging, inspection |
obj.class == SomeClass | Exact class match | No | Rare cases where exact class is required |
obj.is_a?(SomeClass) | Class or subclass match | Yes | Most type checks |
obj.kind_of?(SomeClass) | Same as is_a? | Yes | Same use as is_a? |
obj.respond_to?(:method_name) |
Cheat Sheet
# Exact class
obj.class
# Exact class comparison
obj.class == String
# Class or subclass check
obj.is_a?(String)
obj.kind_of?(String)
# Behavior check
obj.respond_to?(:each)
Quick rules
.classreturns the exact class.is_a?andkind_of?include inheritance.- Prefer
is_a?over.class == ...in most type checks. - Prefer
respond_to?when behavior matters more than class.
Examples
1.class # Integer
1.is_a?(Integer) # true
1.is_a?(Numeric) # true
1.class == Numeric # false
"hi".respond_to?()
FAQ
Is .class the proper way to determine an object’s type in Ruby?
Yes. .class is the correct way to get an object's exact class. But for many checks, is_a? is more useful.
What is the difference between .class and is_a? in Ruby?
.class returns the exact class. is_a? returns true if the object belongs to that class or any subclass.
Should I use kind_of? or is_a??
They are effectively the same for normal use. is_a? is more commonly preferred because it reads more naturally.
Why does .class == ParentClass sometimes return false?
Because .class only checks the exact class. If the object is an instance of a subclass, the comparison to the parent class is false.
Is checking type the Ruby way?
Sometimes, but Ruby often prefers checking behavior with instead of checking exact classes.
Mini Project
Description
Build a small Ruby utility that inspects different values and reports useful type information. This helps you practice the difference between exact class checks, inheritance-aware checks, and behavior-based checks.
Goal
Create a script that prints an object's class, checks whether it belongs to broader Ruby types, and checks whether it supports selected methods.
Requirements
- Create a method that accepts any Ruby object.
- Print the object's exact class using
.class. - Check whether the object is a
Numeric,String,Array, orHashusingis_a?. - Check whether the object responds to
:eachand:to_s. - Test the method with at least four different Ruby values.
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.