More detailed Ruby symbol learning materials

Source: Internet
Author: User
Tags ruby on rails

The explanation is good. It should be clear a lot.
Ruby symbol details
Cause
Recently I have been studying Ruby on Rails. It is indeed an excellent database development framework. However, a large number of statements similar to the following are found in the rhtml file in the view Folder:
<TD> <% = link_to recipe. Title,: Action => "show",: Id => 1%> </TD>
This is a link. If there is no colon, it means a connection that points to http: // 127.0.0.1: 3000/recipe/show/1, that is, the information of the entry with ID 1 in the "display" database table recipe, but what is puzzling is the colon Before action and ID. What are their functions?

A disadvantage of Ruby's object-oriented features
In Ruby, everything is an object. An example of a simple string:
Ruby-e 'puuts "Hello World". class'
String
The class to which the "Hello World" string belongs is printed. The result shows that it is an instance of a string object. We can also display its object number.
Ruby-e 'puuts "Hello World". object_id'
41436168
This is why Ruby has always advertised that it is completely object-oriented. It is indeed very thorough. But if everything is good, there will be bad. The memory space occupied by an object is obviously much larger than that occupied by pure variables. WhenProgramWhen a large number of strings are involved, a ruby program will occupy too much memory. For example:
We use the hash list to store the song information.
Song1 = {'title' => 'used to love you', 'artlist' => 'John legend '}
Song2 = {'title' => 'I still', 'artlist' => 'backstreet boys '}
#......
# Many songs, only two songs are used here
For I in 1 .. 2
Thesong = "Song" + I. to_s
Eval <-Proc
# {Thesong}. each_key {| key | puts key. object_id.to_s}
Proc
End
Result:
41436144
41436408
41435904
41436000
Because object_id is different, each key in the hash table is an independent String object. Even if the content is the same (such as 'title'), Ruby regards it as a different object, in this way, a lot of memory is occupied for no reason. But in fact, in most cases, we only regard the key in hash as a field and will not involve the method of the string class. Ruby will automatically set it as an object, which is suspected of being a zombie.

What is symbol?
In Ruby, a colon is followed by a string. Obviously, according to the law that "Everything is an object", it is also an object.
Ruby-e 'puts: Action. class'
Symbol
This object solves the problem of excessive memory usage caused by "same content string and different objects. Simply put, action represents the 'Action' string, which is a string rather than a String object.
Ruby-e 'puuts: action'
Action
More specifically, a symbol object represents the string after the colon of the object.
Ruby-e 'puuts: action'
Action
Ruby-e 'puts: "Hello World "'
Hello World
All strings with the same content can be replaced by only one marking object, which reduces unnecessary object creation and memory usage. However, as I emphasized, "symbol represents strings, not objects", so do not want to mark methods such as capitalize and center that can use string classes, if used, only an error report with an undefined method is returned:
Ruby-e 'puts: Action. capitalize'
-E: 1: Undefined method 'capitalize' for: Action: symbol' (nomethoderror)
Fortunately, symbol provides the Conversion Function to_s to generate a String object, which extracts the string content and upgrades it to an object.
Ruby-e 'puts: Action. to_s.capitalize'
Action
In addition, it is very important that there is no way to assign values to symbol. In other words, once defined, the value cannot be changed.
Ruby-e': Action = "hello "'
Syntax Error
Unfortunately, even if to_s is used, the assignment still cannot proceed smoothly, because Ruby considers "to_s =" as an undefined function. Unless a reference is explicitly specified for the string object to be converted (but the point of the connection has actually changed after replication ):
: Action
Myaction =: Action. to_s
Myaction = "Lala"
Puts myaction
Result:
Lala
How to Use symbol
Any place where symbol can be used can use the corresponding String object. Create a method similar to JavaBean in rails:
Attr_reader: Action
It creates a method to read the instance variable @ action, which can also be written as follows:
Attr_reader "action"
Otherwise, as long
The string does not need to be changed during the program running.
The string class method is not required.
Therefore, we can safely use symbol to replace string objects, which greatly reduces memory usage, especially in rails. Because data needs to be frequently redirected and transferred between various control methods and pages, a large number of method names are replaced by symbol, which saves the memory and improves the running speed.

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.