First, Methods,private_methods is an instance method of the object class; Instance_methods is an instance method of the module class.
Let's start by looking at the reasons for this arrangement:
We know that the method that a Ruby object can invoke is contained in its ancestor chain (the Singleton class that contains the object). The Ruby objects mentioned here can be divided into 2 classes, such as ordinary objects, such as "ABC", 2,obj=object.new objects, which belong to the class string, Fixnum,object, we call this object a normal object, and a class of objects that are classes (the class itself is also an object), such as String,class, which is also an object, and the classes they belong to are classes, which we call the class object.
The ancestor chain of ordinary object, take "ABC" as an example, for string-> comparable->object->kernel-> Basicobject
The ancestor chain of the class object, in string as an example, for class->module->object->kernel-> Basicobject
We can see that there is no Instance_methods method for ordinary objects because there are no module classes on their ancestor chains. So for an ordinary object, we can only say that it has a method or a private method, but not that it has an instance method, the instance method is for a class.
The class object has a module class on its ancestor chain, so it has instance_methods, and we can say that the class has an instance method.
In addition, the methods of an ordinary object and the instance_methods of its owning class are generally equal. "ABC". Methods = = String.instance_methods because the method of the ordinary object is the instance method of the class to which it belongs.
This is generally because if an instance method is defined in a singleton class of an ordinary object, the methods of the ordinary object will be more than the instance method of the class to which it belongs. Examples are as follows:
obj = string.new ("abc")
Obj.instance_eval {
def method1
"Method1"
End
}
P Obj.methods = = String.instance_methods//false
Finally, the methods method returns the Public,protected method of the object, so there is also a Private_methods method to return its private method.
Analysis of Methods,private_methods and Instance_methods in Ruby