Well explained, it should be clear that a lot of
Ruby Symbol detailed
Cause
The recent learning of Ruby on Rails is indeed an excellent database development framework. In the process, however, it is found that there are a number of statements similar to the following in the rhtml file in the View folder:
<td><%= link_to Recipe.title,: Action => "Show",: ID => 1%></td>
This is a point to the link, if there is no colon the meaning of the sentence is very good to understand: this is a connection to HTTP://127.0.0.1:3000/RECIPE/SHOW/1, that is, "display" database table recipe "id" 1 of the entry information, But what's puzzling is the colon in front of the action and ID, what do they do?
A disadvantage of Ruby object-oriented features
In Ruby, everything is an object. For a simple string example:
Ruby-e ' puts "Hello World". Class '
String
This prints the class that the string of "Hello World" belongs to, and the result shows that it is an instance of a string object. We can also display its object number.
Ruby-e ' puts "Hello World". object_id '
41436168
This is why Ruby has always advertised itself as a complete object oriented, and it does so thoroughly. But everything is good and bad, an object occupies a much larger memory space than a pure variable, when a program involves a large number of strings, a ruby program consumes too much memory. As an example,
We use a hash list to store the song's information.
song1 = {' title ' => ' Used to love ', ' Artist ' => ' John Legend '}
Song2 = {' title ' => ' I still ', ' Artist ' => ' Backstreet Boys '}
#......
#很多歌, it's only two.
For I in 1..2
Thesong= "Song" +i.to_s
Eval <<-proc
#{thesong}.each_key {|key| puts key.object_id.to_s}
PROC
End
Results:
41436144
41436408
41435904
41436000
Because the object_id are different, each key in the hash table is a separate string object, even if the content is the same (such as ' title '), Ruby still treats it as a distinct object, which takes up a lot of memory. But in fact, in most cases, we only see the key in the hash as a field, not the String class method, and Ruby automatically sets it as an object with overkill.
What is symbol?
A literal is a "symbol", in which Ruby is shaped like this: action, a colon followed by a string. Obviously, according to the law of "Everything is Object", it is also an object.
Ruby-e ' Puts:action.class '
Symbol
The meaning of this object is that it solves the problem of excessive memory consumption caused by "the same content string, different objects". To put it simply: the action represents the ' action ' string, which is a string, not a string object.
Ruby-e ' Puts:action '
Action
More specifically, a symbol object represents the string after the colon of the object.
Ruby-e ' Puts:action '
Action
Ruby-e ' puts: "Hello World"
Hello World
All strings of the same content need to be replaced with only one tag object, which reduces unnecessary object creation and memory footprint. But, as I emphasized, "symbol represents a string, not an object," so do not want tags to use methods such as capitalize,center, such as the string class, if used, only the error reports that are not defined by the prompt method are available:
Ruby-e ' Puts:action.capitalize '
-e:1: Undefined method ' capitalize ' For:action:Symbol ' (nomethoderror)
Fortunately, symbol provides a transformation function to_s is used to generate a string object that extracts the contents of the string and upgrades it to an object.
Ruby-e ' Puts:action.to_s.capitalize '
Action
Also, it is important to note that symbol has no assignment method, in other words, symbol will not change once it is defined.
Ruby-e ': action= "Hello"
Syntax error
Unfortunately, even with to_s, the assignment is still not going well because Ruby thinks "to_s=" is an undefined function. Unless you explicitly specify a reference to the string object that was generated by the transformation (but in fact the point of the connection has changed after replication):
: Action
myaction=:action.to_s
myaction= "Lala"
Puts Myaction
Results:
Lala
How to use Symbol
Any place where you can use symbol can use the string object that corresponds to it. There are ways to build similar javabean in rails:
Attr_reader:action
It creates a way to read the instance variable @action, or it can be written like this:
Attr_reader "Action"
Conversely, as long as
String is not changed while the program is running
Strings do not have to use string class methods
Then we can safely use symbol to replace the string object, thus greatly reducing the memory footprint, especially in rails. Because of the need to frequently jump and outgoing data between control methods and pages, a large number of method names are replaced by symbol, which saves memory and improves running speed.
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.