Object#is_a?
obj.is_a?(klass) -> true or false Returns:
boolean · Updated March 13, 2026 · Core Classes types class object checking
is_a? is a Kernel method that checks whether an object belongs to a specific class or any of its ancestor classes (superclasses). It’s essential for type checking and implementing polymorphic behavior in Ruby.
Syntax
obj.is_a?(klass)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
klass | Class or Module | — | The class or module to check against |
Examples
Basic type checking
"hello".is_a?(String)
# => true
"hello".is_a?(Integer)
# => false
123.is_a?(Numeric)
# => true
Checking against superclasses
is_a? returns true if the object is an instance of the class or any of its superclasses:
[1, 2, 3].is_a?(Array)
# => true
[1, 2, 3].is_a?(Object)
# => true
[1, 2, 3].is_a?(Hash)
# => false
Checking included modules
is_a? also returns true for modules included in the object’s class:
[1, 2, 3].is_a?(Enumerable)
# => true
"hello".is_a?(Comparable)
# => true
Using with conditional logic
def process(item)
if item.is_a?(String)
puts "Processing string: #{item.upcase}"
elsif item.is_a?(Array)
puts "Processing array with #{item.length} elements"
elsif item.is_a?(Hash)
puts "Processing hash with #{item.keys.length} keys"
else
puts "Unknown type: #{item.class}"
end
end
Checking for multiple types
def numeric?(value)
value.is_a?(Integer) || value.is_a?(Float)
end
numeric?(42) # => true
numeric?(3.14) # => true
numeric?("hi") # => false
Common Patterns
Guard clauses
def greet(user)
return unless user.is_a?(User)
puts "Hello, #{user.name}!"
end
Type-based dispatch
def serialize(value)
case value
when String then value
when Array then value.map { |v| serialize(v) }
when Hash then value.transform_values { |v| serialize(v) }
else value.to_s
end
end
Testing and validation
def create_user(attrs)
raise ArgumentError, "expected Hash" unless attrs.is_a?(Hash)
# ...
end
Errors
Passing a non-Class argument raises a TypeError:
"hello".is_a?("string")
# => TypeError: class or module required
To check if something matches a module directly, use Module#=== directly:
String === "hello" # => true
See Also
- Object#kind_of? — Alias for is_a?
- Object#instance_of? — Checks exact class only (not ancestors)
- Object#respond_to? — Checks if an object responds to a method