Implementation example of ruby declarative syntax and ruby declarative example

Source: Internet
Author: User

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

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.