Callback and hook methods in ruby

Source: Internet
Author: User

In Ruby, callback and hook methods are called when a specific event occurs. There are several types of events:

  • Call a method that does not exist.
  • Class mixing includes a module
  • Define subclass of A Class
  • Add an instance method to the class
  • Add a singleton method to an object
  • Reference a non-existent constant

You can write a callback method for the above events. When this event occurs, this callback method is executed. These callback Methods target an object or a class, rather than the global one.

 

The following are some examples:

  1 method_missing: blocks unidentifiable messages

As mentioned above, a nomethoderror exception is thrown if Method Search is performed based on the object model. Unless a method named method_missing is defined.

As follows:

1 class C2     def method_missing(m)3         puts "there is no method #{m}"4     end5 end6 C.new.hello

Output:

There is no method hello

Class C does not define the instance method Hello (its method search path is not), so method_missing is called.

 

  2. Use Module # embedded ded to capture mixed operations

When a module is mixed into a class (or another module), if the extended ded method of the module has been defined, the method will be called. The parameters of this method are the classes mixed into this module.

As follows:

1 module M2     def self.included(c)3         puts "module M is included by #{c}"4     end5 end6 class C7     include M8 end

Output:

Module M is wrongly ded by C

When module M is mixed into C, the embedded ded method of module M is called.

This method can be used to define class methods. In the code above, class methods of class C can be defined in self. writable ded, or a module can be added to the singleton class.

As follows:

 1 module M 2     def self.included(c) 3         puts "module M is included by #{c}" 4  5         def c.m1 6             puts "this is class #{c}‘s  class method m1 " 7         end 8  9         c.extend(N)10     end11     module N12         def method13             puts "hello world"14         end15     end16 end17 class C18     include M19 end20 p C.singleton_methods

Output:

Module M is wrongly ded by C
[: M1,: method]

For example, line 5-7 defines a class method, which is a class containing module M (C in this example), and line 9 adds module n to the class Singleton class. The Singleton method of output class C in the 20 rows shows that the addition is successful.

 

  3. Use class # inherited to intercept inheritance

When the inherited method is defined for a class, when a subclass is generated for it, inherited will be called, and the unique call parameter is the name of the new subclass.

As follows:

1 class C2     def self.inherited(subclass)3         puts "#{self} got a new subclass #{subclass} "4     end5 end6 class D < C7 end8 class E < D9 end

Output:

C got a new subclass d
D got a new subclass E

When d inherits C, this hook method is called to Output C got a new subclass D. At the same time, the singleton class of D also has the class C method. Therefore, when e inherits D, it will also call the Hook method of D.

 

  4 Module # const_missing

This method is called when an unrecognized constant is referenced in a given module or class.

As follows:

1 class C2     def self.const_missing(const)3         puts "#{const} is undefined-setting "4         const_set(const,1)5     end6 end7 puts C::A

Output

A is undefined-setting
1

Constant A is not defined, so the const_missing method is called. Define it as 1 in the method.

 

  5 Module # method_added

This method is called when a new method is defined.

As follows:

1 module M2     def self.method_added(method)3         puts "method #{method} is added in M"4     end5     def m16     end7 end

Output

Method M1 is added in m

 

There are many hook methods in ruby, covering the vast majority of events worth attention. Here we only provide some common examples for your reference.

 

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.