3

I ran into this behavior earlier today and was hoping somebody could explain why this happens:

class Object
  def some_method
  end
end

Object.respond_to?(:some_method) # => true

Of course, this doesn't happen with other classes:

class Dog
  def some_other_method
  end
end

Dog.respond_to?(:some_other_method) # => false

So what gives?

2
  • class Object is itself an Object. Object.is_a?(Object) Jan 28, 2014 at 21:51
  • @JonathanLonowski Yeah, but so is Dog in this case. Everything is_a?(Object). I'm just confused as to why Object specifically would respond to instance methods defined on it. Jan 28, 2014 at 21:59

1 Answer 1

4

This happens b/c Object is a superclass of Class itself. So class Object is an instance of Object (confusing). When you define an instance method on Dog you dont cause the same issue b/c the class Dog does not appear in the inheritance chain of Class

Object.instance_of?(Class) # True
Class.is_a?(Object) # True
Class.is_a?(Dog) # False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.