rubyguides

Object#instance_of?

obj.instance_of?(klass) -> true or false

instance_of? is a Kernel method that checks whether an object is an instance of exactly the given class — not its superclasses. Unlike is_a? and kind_of?, it does not consider the inheritance chain.

That strictness is both the point and the tradeoff. It is useful when a method needs a very specific class and should reject subclasses, but it is often too rigid for everyday type checks. In many Ruby APIs, subclass compatibility is a feature, and is_a? is usually the more flexible option.

The method is best treated as a contract check. If the caller is allowed to pass only one exact type, instance_of? makes that rule explicit. If the caller can pass a family of related classes, is_a? usually communicates the intent more naturally.

Syntax

obj.instance_of?(klass)

The method takes a single argument — the class to check against — and returns a boolean. It does not accept a module or a string; passing anything other than a Class object raises a TypeError. This restriction keeps the check strict and predictable, which is exactly what you want when the contract demands an exact match.

Parameters

ParameterTypeDescription
klassClassThe exact class to check against

Examples

Exact class matching

"hello".instance_of?(String)
# => true

"hello".instance_of?(Object)
# => false (it's a String, not Object directly)

[1, 2].instance_of?(Array)
# => true

[1, 2].instance_of?(Object)
# => false

These examples show the main idea: the object must match the class exactly. That means the check is predictable, but it also means the method can reject values that would otherwise behave correctly through inheritance. In day-to-day Ruby code, this tradeoff matters most when a class should be treated as a leaf node in the hierarchy, with no expectation that subclasses will provide drop-in replacements. The rigidity can feel unusual at first, but it serves a clear purpose when correctness depends on the exact type rather than on duck-typing compatibility.

the key difference from is_a?

This is where instance_of? differs from is_a?:

num = 42

num.is_a?(Integer)       # => true
num.is_a?(Numeric)       # => true (Numeric is superclass)
num.is_a?(Object)        # => true (Object is root)

num.instance_of?(Integer)   # => true
num.instance_of?(Numeric)   # => false (it's an Integer, not Numeric)
num.instance_of?(Object)    # => false (it's an Integer, not Object)

This comparison is the quickest way to see why instance_of? is stricter than is_a?. The object still belongs to the inheritance tree, but the method only accepts the precise class passed in.

practical use case

Use instance_of? when you need strict type checking that ignores inheritance:

class Config
  def initialize(data)
    # Must be exactly a Hash, not a subclass
    unless data.instance_of?(Hash)
      raise ArgumentError, "expected Hash, got #{data.class}"
    end
    @data = data
  end
end

That kind of check is helpful when an API depends on a very specific object shape and would rather fail early than guess how to coerce a value. In day-to-day Ruby code, though, that strictness is often more of a guardrail than a default choice.

When to use instance_of?

  • Use strict type checking when you need to reject subclasses.
  • Use it in security-sensitive code when object type matters exactly.
  • Use it for API contracts when a method requires a specific class, not a compatible one.

Most of the time, is_a? is more flexible and appropriate since subclasses typically behave the same as their parents.

When you do choose instance_of?, treat it as a contract check rather than a convenience check. It is best when the distinction between a parent class and a subclass matters to the code path itself.

If you are documenting a public method, it can help to explain why the exact class matters. That gives the reader a reason for the strictness instead of making the check look arbitrary.

Errors

Passing a non-Class raises TypeError:

"test".instance_of?("string")
# => TypeError: class or module required

The method is small, but the reasoning around it is important. If the goal is to answer “does this object behave like the thing I need?”, use is_a?. If the goal is “is this object exactly that class?”, instance_of? does the stricter job.

See Also