Analysis of Methods,private_methods and Instance_methods in Ruby

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.