In the previous article, we talked about callable objects proc and Lambda in Ruby. They are all block-converted objects. There are methods for calling objects in ruby. You can obtain a method object by using the method and using the method name as the parameter (string or symbol.
As follows:
1 class C2 def talk3 p "hello world"4 end5 end6 7 c = C.new8 meth = c.method :talk9 meth.call
Output: "Hello World"
Class C has an instance method talk. Class C instance c calls method and uses: talk as a parameter to obtain a method object meth. Then meth calls call and outputs "Hello World ".
It is worth noting that the meth method object is a method bound to object C. When calling a call, C also knows that it is calling the talk method.
You can use the method # unbind () method to separate a method from the object it is bound to. This method returns an unboundmethod object and cannot execute the unboundmethod object, you must bind it to an object to make it a method object again.
As follows: (based on the above Code)
1 class D < C2 end3 d = D.new4 unbound = meth.unbind5 new_meth = unbound.bind(d)6 new_meth.call
Output: "Hello World"
After unbind is unbound, it is bound to an instance object d of subclass D of C. Call the method object. If you do not want to unbind a bound method, you can use the following code to unbind the object:
unbound = C.instance_method (:talk)
Why? The existence is always reasonable. Binding and unbinding also have its applicability.
In the previous method search in Ruby, we know that Ruby's method call is called based on the first matching method. So what if we want to call the second method on the matching path? Ruby has the keyword "super. However, adding the super keyword is equivalent to changing this method. It does not directly call the second method on the matching path. Instead, the second method is called in the first matching method. Also, what if we want to call the third matched method on the matching path? What about the fourth? The binding method can be used.
As follows:
1 class C 2 def method 3 p "this is in C" 4 end 5 end 6 7 class D < C 8 def method 9 p "this is in D"10 end11 end12 13 class E < D14 def method 15 p "this is in E"16 end17 end18 19 e = E.new20 e.method21 22 D.instance_method(:method).bind(e).call23 C.instance_method(:method).bind(e).call
Output:
"This is in E"
"This is in D"
"This is in C"
As shown in the code, Class C, D, E, and D inherit from Class C and Class E inherit from Class D. They all define the method. All three methods in the search path of instance E can be called. If you call E. method directly. So it must have called the first matching method, that is, the method in Class E, such as the first line of output. What if I want to call the method in Class D? For example, 22 lines of code. Bind the instance method of D to Object E, and then call it. Call the method in Class C. For example, 23 rows.
If you have learned C ++, you may think that the pointer of the derived class references the base class object. A derived class is a special case of a base class. Therefore, only objects of the base class can be referenced after the forced conversion type. Similar to the binding in ruby, the method of the parent class can be called only after it is bound to the method of the parent class.
Make a summary with the previous article. Objects that can be called in Ruby include:
Blocks (not called objects), Proc (Objects of the proc class), and Lambda (Objects of the proc class) are all executed in the scope of defining them.
Method: bind to an object and execute it in the scope of the bound object.
Methods for converting an object to another object include: Proc. New (), method # to_proc (), & operator