Excerpts from Arup Rakshit's message of 2014-01-29 15:52:43 +0100:
Code :
irb(main):001:0> class Animal
irb(main):002:1> def bark;end
irb(main):003:1> end
=> nil
irb(main):004:0> class Object
irb(main):005:1> def foo;end
irb(main):006:1> end
=> nil
irb(main):007:0> Animal.respond_to?(:bark)
=> false
irb(main):008:0> Object.respond_to?(:foo)
=> true
irb(main):009:0>
My question is -
I can see `foo` is an instance method of Object. If so Why does
`Object.respond_to?(:foo)` return `true` ?
if I understand this `Animal.respond_to?(:bark) # => false`, I can't
understand `Object.respond_to?(:foo) # => true` .
Because everything in ruby is an instance of Object, including instances of class Class
(that is, including classes). Since your foo method is an instance method of
Object, it can be called on everything. You can check this by calling: Animal.foo. It
won't raise exceptions.