Implementation example of ruby declarative syntax and ruby declarative example
ActiveRecord can be easily declared to define the associations between models. For example:
The Code is as follows:
Class Topic <ActiveRecord: Base
Has_timeout: posts
Belongs_to: user
End
Has_many and belongs_to are actually the class method of the Topic class. The standard syntax is:
The Code is as follows:
Class Topic <ActiveRecord: Base
Topic. has_many (: posts)
Topic. belongs_to (: user)
End
So what does has_many bring to us? When the class method has_many is executed, a series of methods are added to the Topic object instance: posts, posts <, orders. push... and so on. So when we declare object relationships such as has_many and belongs_to in the model, a series of related object methods will be automatically added. Let's try it on our own:
The Code is as follows:
Module M
Def self. Sorted ded (c)
C. extend (G)
End
Module G
Def generate_method (* args)
Args. each do | method_name |
Define_method (method_name) {puts method_name}
End
End
End
End
Class C
Include M
Generate_method: method1,: method2
End
C = C. new
C. method1
C. method2
We have defined a declaration generate_method that can accept multiple symbrs to dynamically create methods with the same name. Now we use this declaration in Class C: generate_method: method1,: method2. Of course we need to include module M. Why do ActiveRecord models do not need to include related modules? Of course, it is because the Topic's parent class ActiveRecord: Base has included the module Associations.
Class C calls an extended ded callback interface of the module M through the include module M, so that class C goes to the extend module G. In other words, the include module M, to dynamically Add a class method generate_method to Class C.
This generate_method is defined in module G. It accepts a series of parameters to dynamically create related methods. So we implemented the DSL function:
By declaring generate_method: method1,: method2 in Class C, let Class C dynamically add two instance methods method1 and method2. Isn't that interesting? In fact, the object association declaration of rails is also implemented in the same way.
Ruby Syntax problems
@ Cache | ={} if @ cache already has data, no action will be taken.
Equivalent to @ cache | @ cache = {}
That is
If (@ cache ){}
Else {@ cache = {}}
@ Cache ={} indicates that no matter whether @ cache has any information,
Will be cleared, back to the initialization status
A Class Example of ruby.
1. def [] = (key, value): defines an instance method, allowing the instance to use MyMap # [] = method. Simply put, after you define this method, you can assign values like this, m ["key"] = 12345 => [{"key" => 12345}], this method stores {"key" => 12345} in the array @ status.
2. status | = []: equivalent to status = status | [], or operation. If the status is not nil, status is assigned. If the status is nil, [] is assigned.
3. def [] (key): If the method just [] = is set value assignment, then this method is get display value, m ["key"] = 12345 is used to store a hash in the array. This shows the hash value. After this value is assigned, m is [{"key" => 12345}]. This method is to read the value of the hash element in the array. For example:
M ["clue"] = 54321 # m becomes [{'key', 12345}, {'clue ', 54321}]
Puts m ["clue"] # displays 54321