Dynamic invocation of Ruby Learning

Source: Internet
Author: User

As a dynamic language, the method in the object does not need to be verified as a static language, and the object of the dynamic language keeps talking, and if you call a method that has not been defined, the program does not immediately error and cannot run, only when it is run until you call this method. The interpreter cannot continue to interpret because the method could not be found. And before that, you can add the method in the process of running it. You can even use a method to handle all the methods that have not been defined, and to make some reactions.

Method repeat

Cite an example in the book, there is a quotation system, you need to read from the database of various instruments and equipment information, prices, such as the purchase of a computer, need to read CPU, mouse, keyboard and other information. You may need a mouse () method to read the model of the mouse as well as the price, a CPU () method to read the CPU model as well as the price, a keyboard () method to read the keyboard model as well as the price and so on. The contents of these methods are similar, all from the database to execute the corresponding search statements, but the search statements are different, this similar but not the same duplicate code will make the whole program looks very cool, from the principle that these methods are executed the same process, you should have a technology to eliminate these duplicate code.

In Ruby, there are two techniques for doing this: dynamic and Ghost methods.

Dynamic Method Dynamic Creation

In the Module class, there is a Define_method method that the Module#define_method method can dynamically create by passing in parameters and code blocks:

Class computer
? ? def self.define_component (name)
? ? ? ? Define_method (name) {
? ? ? ? Puts "getting #{name} info"
? ? ? ? Puts "getting #{name} price"
? ? }
? ? End
End
Computer.define_component:keyboard
obj = computer.new
Obj.keyboard

As shown above, the keyboard method is not defined in computer, but the keyboard method can be dynamically created elsewhere in the code through the Define_method method, or COMPUTER.DEFINE_COMPONENT:CPU, How to create a CPU.?

Dynamic invocation

The work of dynamic creation is completed, and the next step is to implement dynamic invocation. In the Object class, Ruby encapsulates a Send method, which is called by the Object#send method through the arguments passed in. In each device's method, the invocation of the query statement is not the same, such as keyboard should call Data_source.get_keyboard_info,data_source.get_keyboard_price and other methods, But the CPU needs to call the Data_source.get_cpu_info,data_source.get_cpu_price method, through the Object#send method, can implement the call to this kind of method. Such as:

Obj.send:keyboard

You can invoke the keyboard method in obj, which enables dynamic invocation of other methods. Send can also pass the method parameter: Obj.send "Method_name", para.

Ghost method: Method_missing ()

Method_missing () is a method of the kernel module, and all objects inherit from kernel, so all objects have a method_missing () method.

The Method_missing () method is called when the method is not found and returns an incorrect message, and by overwriting the method_missing, it is possible to perform a uniform operation when the method does not exist:

Class MyClass
? ? def method_missing (name)
? ? ? ? Puts "getting #{name} info"
? ? ? ? Puts "getting #{name} price"
? ? End
End
obj = MyClass.New
Obj.cpu

But method_missing () is not free to use, think carefully about the above code, there are a lot of problems, such as will call you do not want to call some methods, such as Pig.fly, in the above problem, if the database does not have some data, for example, book, if the use of Ghost method to invoke, it will cause errors. So you need to add respond_to? () method to confirm that the database responds correctly to the calling method, and that the Ghost method also considers some variables to be methods because they are unrestricted and requires a restriction on the scope of the method, and that in the ancestor chain of an object there may be methods that you expect to solve with a phantom approach, and in fact, Because the interpreter found that method, although it is not the method you want, but still executes the found method, you need to delete some inherited methods, or inherit basicobject to clear all inherited methods and so on.

Ghost method is cool, but must be used cautiously, conservative I seem to prefer to use dynamic methods to solve the way.

Dynamic invocation of Ruby Learning

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.