When I started to learn Ruby, I couldn't figure out the difference between the symbol (the variable needs to add:) and the string, in order to design such a thing for the language. It makes me confused.
First, the string object is different. For example, "string" "string" is different, the instance is different.
"String". equal? ("String")
This function returns FALSE.
But the same name symbol, the instance is the same
: Str.equal? (: Str)
This function returns TRUE.
So what's the effect? If you create a hash table:
Hash_tab = {
"One" = 1,
"Both" = 2,
.......
}
When we facilitate this watch, each loop, but does not change the key, it will create a lot of new string objects, will increase the cost of the processor, then you can change to symbol.
Hash_tab = {
: one = 1,
: both = 2,
.......
}
Of course, both symbols and strings can be used to represent text in a way. They can also be converted freely between them.
References
http://blog.csdn.net/besfanfei/article/details/7966987
http://blog.csdn.net/besfanfei/article/details/7966850
Symbol and string in Ruby