How many methods do you have to create a method? Most people may think of the def keyword Ruby. Code
- # Common method
- DefTele_you
- Puts"I am anleb"
- End
# General method def tele_you puts "I am anleb" End
Ruby code
- # Define a single-piece Method
- N ="Anleb"
- DefN. tell_you
- Puts"I am # {self }"
- End
- N. tell_you
# Define the single-piece method N = "anleb" def N. tell_you puts "I am # {self}" endn. tell_you
Ruby code
- # define_method
- class project
- define_method : tell_you DO | Name |
- puts name
- end
- end
-
- A = project. New
- . tell_you ( "anleb" )
# Define_methodclass project define_method: tell_you do | Name | puts name endenda = project. newa. tell_you ("anleb ")
Specific analysis: define_method Ruby code
- Kernel. private_methods.include? ("Define_method")# True
Kernel. private_methods.include? ("Define_method") # True
We can see that define_method is a private method, so there are rules for calling Private methods:
1. There cannot be explicit calls, that is, there cannot be recipients, and there cannot be calls like self. define_method.
2. Private methods can be inherited.
3. Private methods can be forcibly called by "send", for example, "Send (: private_method)
4. Only private methods can be called in itself (understand the meaning of this sentence)
In the project class above, the current self is a class project, and then the define_method method is called implicitly to createCubeMethod
Define_methods dynamic creation method. What is dynamic creation? It means that a method is created and defined during code runtime. Using Dynamic Creation and dynamic distribution, Ruby code reconstruction can be realized. This is magic!
The example meta-programming book contains 46 Chinese versions and 69 English versions.
Let's look at an example:
-
- ClassProject1
-
- Define_method : Tell_you Do| Name |
-
- Puts name
-
- End
-
- End
-
-
- ClassProject2 <project1
-
- End
-
-
- A = project2.New
-
- A. tell_you ("Anleb")
-
- Project1.instance _ methods (False)# ["Tell_you"]
Class project1 define_method: tell_you do | Name | puts name endendclass project2 <project1 ENDA = project2.newa. tell_you ("anleb") project1.instance _ methods (false) # ["tell_you"]
1. It indicates that the methods defined by define_method are no different from those defined by def and can be inherited.
2. The define_method method is an instance method that exists in the class.
Code modification: Ruby code
- ClassProject1
-
- Define_method : Tell_you Do| Name |
-
- Puts name
-
- End
-
-
- DefCreat_method
- Define_method : New_method Do
-
- Puts"This is a new method"
-
- End
-
- End
-
- End
-
- Project1.New. Creat_method
-
-
- Error: Test. RB: 7: In'Creat _ Method': Undefined method' DEFINE _ Method' For # <Project1: 0x2bc7008> (nomethoderror)
Class project1 define_method: tell_you do | Name | puts name end def creat_method define_method: new_method do puts "this is a new method" End endendproject1.new. creat_methoderror: test. RB: 7: In 'creat _ method': Undefined method 'define _ method' for # <project1: 0x2bc7008> (nomethoderror)
I forgot to say that define_method is the object class method, that is, only the class can be called,
The current self of creat_method must be an object. The object is not a class, so it cannot be called. modify the code.
- def creat_method
- Self . class . define_method : new_method DO
- puts "this is a new method"
- end
- end
Def creat_method self. Class. define_method: new_method do puts "this is a new method" end
Call method:
- Project1.New. Creat_method
- Error: Test. RB: 7: In'Creat _ Method': Private method 'define _ Method'CalledForProject1:Class(Nomethoderror)
Project1.new. creat_methoderror: Test. RB: 7: In 'creat _ method': Private method 'define _ method' called for project1: Class (nomethoderror)
Crash, why not? Let's take a look at the prompt, saying this is a private method, amount... Forget that the private method cannot explicitly have a receiver. Let's think of a method, right, that is, the send method.
Modify the Code:
-
- ClassProject1
-
- Define_method : Tell_you Do| Name |
-
- Puts name
-
- End
-
-
- DefCreat_method
- Self.Class. Send (: Define_method,: New_method)Do
-
- Puts"This is a new method"
-
- End
-
- End
-
- End
-
- A = project1.New
-
- P project1.instance _ methods (False)# ["Creat_method", "tell_you"]
-
- A. creat_method
-
- P project1.instance _ methods (False)# ["Creat_method", "tell_you"]
Class project1 define_method: tell_you do | Name | puts name end def creat_method self. class. send (: define_method,: new_method) do puts "this is a new method" End endenda = project1.newp project1.instance _ methods (false) # ["creat_method", "tell_you"]. creat_methodp project1.instance _ methods (false) # ["creat_method", "tell_you"]
Finally, it succeeded. This technology is called the dynamic distribution technology, which is commonly used in the method_messing method.
Solution 2: Java code
-
- ClassProject1
-
- Define_method: tell_youDo| Name |
-
- Puts name
-
- End
-
-
- Def self. creat_method
-
- Define_method: new_methodDo# The current self is a class, so it can be called and implicitly
- Puts"This is a new method"
-
- End
-
- End
-
- Creat_method
-
- End
-
-
- Project1.New. New_method
Class project1 define_method: tell_you do | Name | puts name end def self. creat_method define_method: new_method do # The current self is a class, so it can be called, and the implicit puts "this is a new method" end creat_methodendproject1.new.new_method
Solution 3: Ruby code
-
- ClassProject1
-
- Class<Self
-
- DefCreat_method
-
- Define_method : New_method Do# Current self is a class
- Puts"This is a new method"
-
- End
-
- End
-
- End
-
- Creat_method
-
- End
-
-
- Project1.New. New_method
Class project1 class <self def creat_method define_method: new_method do # The current self is a class puts "this is a new method" end creat_methodendproject1.new.new_method
The research is not good. If you have any mistakes, please contact us!