Find and execute methods in Ruby, and search and execute in ruby

Source: Internet
Author: User

Find and execute methods in Ruby, and search and execute in ruby

When you call a method, Ruby does two things.

1. Find this method. This method is called method lookup.

2. Execute this method. To do this, Ruby needs something called self.

Such a process-discovering and executing a method-occurs in every object-oriented language. However, it is important to have a deep understanding of a very dynamic language like Ruby. Have you ever wondered where a method is defined? If so, you must have a deep understanding of Method Search and self.

When a method is called, Ruby searches for the method in the class of the object. However, before providing more complex examples, you need to understand two new concepts: receiver and ancetors chain ).

The recipient is the object where you call the method. For example, in the my_string.reverse () Statement, my_string is the receiver.

To understand the concept of the ancestor chain, you can first observe any Ruby class. Imagine moving from a class to its superclass, then moving to the superclass of the super class, and so on until it reaches the Object class (the default superclass of all classes ), finally, we came to the BasicObject class (the root node of the Ruby class architecture ). In this process, the class path you experience is the class's ancestor chain (the ancestor chain can also contain modules ).

Now that you know what the receiver and the ancestor chain are, you can use one sentence to summarize the process of searching for a method: to find a method, Ruby first searches for the class of the receiver, then, search in the ancestor chain layer by layer until this method is found.

class MyClass  def my_method    my_method()  endend class MySubclass <MyClassend obj = MySubclass.newobj.my_method()              # =>"my_method()"

When the my_method () method is called, Ruby starts from the receiver obj and comes to the MySubclass class. Because the my_method () method cannot be found here, Ruby comes up to the MyClass class and finds this method there.

If this method is not found here, Ruby will follow the ancestor chain up to the Object class and BasicObject class. In the order of drawing by most people, this search behavior is called the "one to the right, then up" rule. That is to say, first step to the right to the class where the receiver is located, and then move up along the ancestor chain until the given method is found.

So far, we have only focused on how to find a method, and now we have to look at how to execute the method.

Imagine you are the Ruby interpreter. If someone calls a method named my_method (), you can find this method by "step to the right". The definition of this method is as follows:
 

def my_method  temp = @x +1  my_other_method(temp)    end

To execute this method, you need to answer two questions. First, which object does the instance variable @ x belong? Secondly, on which object should you call the method my_other_method ()?

As a wise human (rather than stupid computer program), you may be able to answer these two questions intuitively: @ x instance variable and my_other_method () all methods belong to the receiver -- the object that initially calls the my_method () method. However, Ruby does not have intuition, which is a luxury for it. When calling a method, Ruby needs to hold a reference from the receiver. It is the existence of this reference. It can remember which object is the receiver and use it to execute this method. This recipient reference can also be used for you.

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.