More detailed ruby symbol learning materials

Source: Internet
Author: User

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 a bad thing. The memory space occupied by an object is obviously much larger than that of pure variables. When a program involves a large number of strings, A ruby program occupies 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.