4

instance_methods is defined as a public instance method within the Module class. Why and how are we then able to call Object.instance_methods, which is the syntax for class method invocation?

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
don007
  • 53
  • 4
  • possible duplicate of [Can I invoke an instance method on a Ruby module without including it?](http://stackoverflow.com/questions/322470/can-i-invoke-an-instance-method-on-a-ruby-module-without-including-it) – Rustam Gasanov Dec 23 '14 at 05:09

3 Answers3

5

Because instance_methods is a instance method on Module, that method can be called on any instance of the Module class or it's subclasses.

As it turns out, Object is an instance of the Class class:

Object.instance_of? Class
#=> true

And, Class is a subclass of Module:

Class < Module
#=> true

Here's a helpful chart illustrating the class hierarchy of the various objects in Ruby. Notice how Module is listed as a superclass of Class, which all Classes in Ruby are instances of:

Ruby class hierarchy

View full size

Ajedi32
  • 45,670
  • 22
  • 127
  • 172
  • Where is this graph coming from, was it ploted specifficaly for this answer? – psychoslave Dec 03 '21 at 15:43
  • 1
    @psychoslave I don't remember now, but it seems to be from this guy on the Ruby forums? https://www.ruby-forum.com/t/the-ruby-object-model/237824 Not sure why I didn't cite the source all those years ago, but there it is. – Ajedi32 Dec 06 '21 at 16:18
  • Thank you @ajedi32. Too bad the working version on giffy seems now unavailable. Having a version we could update if required would be nice. – psychoslave Dec 07 '21 at 10:19
0

It looks like a class method in this case, but in Ruby, Object is just an instance of Class, which has Module as a super class. So what looks like a class method here is actually an instance method invoke on instance Object of class Class.

Object.instance_of? Class # => true
Object.is_a? Module #=> true 
Vu Minh Tan
  • 526
  • 4
  • 6
0

There is no such thing as the "syntax of class method invocation". There is also no such thing as a "class method".

This is just method invocation like any other method invocation. You are calling the method instance_methods on the object referenced by the constant Object. That object is the Object class, which is an instance of the Class class. Class is a subclass of Module, and so the Object class is an (indirect) instance of the Module class which defines the instance_methods method.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • How can you state that there is no such thing as a class method. There are class and instance methods for almost all classes in ruby. Also there is literally a Class named Method ;) – engineersmnky Dec 23 '14 at 23:54
  • @engineersmnky: No, there are no class methods. What we *call* "class methods" are actually just singleton methods on the object which happens to be a class. But actually, there are no singleton methods either, what we *call* "singleton methods" are actually just regular old instance methods of the singleton class. So, "class methods" are actually just regular old instance methods of the singleton class of an object which just happens to be a class. – Jörg W Mittag Dec 23 '14 at 23:59
  • In that event shouldn't ::singleton_methods return the same as say ::methods? How does BasicObject fall into this. I am not trying to be argumentative I just find this interesting. – engineersmnky Dec 24 '14 at 00:23