Method Search in ruby

Source: Internet
Author: User

Method Search in ruby
Method calls in ruby are in the form of objects. Methods. How can an object find this method? First, you must understand the concept of the ancestor chain. The ancestor chain starts from a class to its parent class, and then to the parent class of the parent class... until the final starting point (the BasicObject class in ruby ). During this period, the path that we experienced was the ancestor chain. 1. Search for an instance object by mixed modules and inheritance methods. First, check whether there are corresponding instance methods in the class to which the object belongs. Then, check whether there are modules in the class. If yes, check whether there are corresponding methods in the module. If not, find the parent class. First, let's look at the instance method of the parent class, then check whether there are modules in the parent class, and then look at the parent class of the parent class... until the end, the BasicObject class and the Kernel module. If not, you can view the method_missing function, which is a built-in function. This function reports an error by default. Of course, you can rewrite this method to process unfound methods. Copy the Code 1 module M 2 def method 3 puts "this is method in module M" 4 end 5 end 6 7 class C 8 include M 9 end10 11 class D <C; end12 13 D. new. method14 p D. ancestors copies the code and outputs this is method in module M [D, C, M, Object, Kernel, BasicObject]. in this example, the Class D Object D. new method: first find the instance method in D, no, and no module in D. The parent class of D is C. Find the method of the C instance first, but there is module M in C, and the method in module M. This order is based on the sequence of the ancestor chain. To what extent can this search be performed? For example, the ancestor chain represents the Object class, which is the beginning of all objects in ruby. There is a module Kernel. That is to say, if you define a method in the Kernel, all objects in ruby can use this method. 2. If multiple methods contain the same method, the first method is matched. As follows: copy code 1 module M 2 def method 3 puts "this is method in module M" 4 end 5 end 6 7 module N 8 def method 9 puts "this is method in module N" 10 end11 end12 13 class C14 include M15 include N16 end17 18 C. new. method19 p C. ancestors copies the code output result. this is method in module N [C, N, M, Object, Kernel, BasicObject] Class C contains two modules: M and N, both M and N have the method. So which one to call? If the method defined in ruby is in the same class, it overwrites the old method. Similarly, if Module N is backward mixed with M, such as class C, the method in N is called. On the other hand, from the perspective of the ancestor chain, N is also at the top of M, so N method is also called first. How are modules sorted in the ancestor chain? In the ancestor chain, a module M is exactly located in the previous position of class C that contains it. In this example, Class C first contains M, which is C and M in the ancestor chain. Then it contains N, N, and exactly in the last position of C, so it becomes C, N, M. 3. The methods that contain Singleton classes look for the first two cases without Singleton classes. If Singleton classes exist, you must first consider Singleton classes. Singleton class: specifically, it is a class unique to an object. It can only belong to one object (even other object instances of the same class), so it is called a singleton class. In ruby, each object actually has two classes: Classes shared by multiple object instances and Singleton classes. The method called by the object is the instance method in the two classes, and the methods in the ancestor class and mixed modules. When there is a singleton class, the object method first searches for Singleton classes, then modules that are mixed with Singleton classes, and then the class to which the object belongs, and so on. The parent class of the singleton class is the class to which the object belongs. Copy code 1 module M 2 def method 3 puts "this is method in module M" 4 end 5 end 6 7 class C 8 end 9 10 c = C. new11 class <c12 def method13 puts "this is method in c 'singleton class" 14 end15 include M16 p ancestors17 end18 19 c. the output of the method copy code is [M, C, Object, Kernel, BasicObject] this is method in c 'singleton class singleton class, which is not displayed in the ancestor chain, however, the called method is indeed a singleton class method. Then there is the mixed module M, then the parent class, and so on. As can be seen from the ancestor chain, the parent class of the singleton class is C, which is the class of Object c. HELP has a problem here. If Class C also contains class module M, the ancestor chain should theoretically be M, C, M, Object, Kernel, BasicObject as follows: copy code 1 module M 2 end 3 class C 4 include M 5 end 6 c = C. new 7 class <c 8 include M 9 p ancestors10 end11 p C. ancestors: [C, M, Object, Kernel, BasicObject] [C, M, Object, Kernel, basicObject] The modules mixed in the singleton class do not appear in the ancestor chain. The Singleton class of c is the same as the ancestor chain of class C. Assume that class C contains not module M, but another module N. Copy code 1 module M 2 end 3 module N 4 end 5 class C 6 include N 7 end 8 c = C. new 9 class <c10 include M11 p ancestors12 end13 p C. ancestors copies the code output result [M, C, N, Object, Kernel, BasicObject] [C, N, Object, Kernel, BasicObject]. At this time, the result is the same as I expected. In the ancestor chain, there are still modules M mixed with c Singleton classes. Why? Does it mean that if the singleton class and other classes on the ancestor chain are mixed with the same module, the module name in the singleton class will not be displayed? In addition, I also include M in the class Object. The result is [C, N, Object, M, Kernel, BasicObject], and the module M in the c Singleton class does not exist. If N is contained, the result is [M, C, N, Object, N, Kernel, BasicObject], which is the same as expected. Is it because the singleton class and other classes on the ancestor chain cannot contain the same module? I know that non-singleton classes can contain modules with the same name and can appear in the ancestor chain at the same time. (I am using ruby1.9.3) I am very grateful for knowing how to answer this question. The Singleton class of the four methods is the singleton class of the Instance Object. What if it is a class Singleton class? (Each object has a singleton class, and classes are also objects. Of course, there are also Singleton classes. Class methods are placed in singleton classes .) A singleton class cannot be inherited, but a singleton class can have a parent class or subclass. Copy code 1 class C 2 def self. method 3 p "This is method in C" 4 end 5 end 6 class D <C 7 end 8 D. method 9 10 en = class <C; self; end11 class E <en; end; copy the code output result "This is method in C" can't make subclass of singleton class (TypeError). The result is displayed. method calls the singleton method of C, indicating that the singleton class of D inherits the singleton class of C and is its subclass. However, from lines 10-11, we can see that the singleton class cannot be inherited. I think we can think that: when D inherits C, D's Singleton class inherits C's Singleton class, so D can call C's class method. Similarly, D can also call the class methods of the Object class. Sort out: Find the Instance Object method of the class, first find the singleton class, and then the module in the singleton class. Find the parent class, the module in the parent class. And so on. Class Object Method Search, first find the singleton class (that is, the class method), then find the singleton class of the parent class, and so on. If multiple methods are found, match the first method. In ruby, a class cannot be inherited, and it can also have subclasses. For example, a singleton class in ruby. If we use superclass to find the parent class, we can get (# represents the singleton class. Suppose d is the object of class D, and Class D inherits Class C,-> indicates that the parent class is) # d-> D-> C-> Object-> BasicObject-> nil # D-> # C-> # Object-> # BasicObject-> Class-> Module-> Object -> BasicObject-> nil

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.