# Best-practice: :symbol || 'string\_literal'?

**URL:** <https://www.ruby-forum.com/t/best-practice-symbol-string-literal/143921>\
**Category:** Rails\
**Created:** [July 23, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/best-practice-symbol-string-literal/143921 "2008-07-23T21:42:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [July 23, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/best-practice-symbol-string-literal/143921/1 "2008-07-23T21:42:05Z")

</div>

I was researching an earlier issue with rails and I stumbled across a  
document which mentioned something that sounded to me like “Due to a  
recent shift in the mind-set of the community, it is frowned upon to  
use a string literal in any situation which does not require text  
manipulation; use symbols instead.” Did I interpret the info on that  
page correctly?  
Also, any further info you can give me about when I should be using  
literals and when to use symbols would be very much appreciated.  
Also, any other tips on best-practice or links to good resources about  
such would be rad.

-Kyle

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [July 23, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/best-practice-symbol-string-literal/143921/2 "2008-07-23T22:01:27Z")

</div>

> I was researching an earlier issue with rails and I stumbled across a  
> document which mentioned something that sounded to me like “Due to a  
> recent shift in the mind-set of the community, it is frowned upon to  
> use a string literal in any situation which does not require text  
> manipulation; use symbols instead.” Did I interpret the info on that  
> page correctly?

No idea, since I don’t know what page you’re referring to, but it  
sounds reasonable.

One reason to use symbols is that there is only _one_ :foo, but there  
are many “foo”'s. That is…

> > :foo.object\_id  
> > =\> 146098
> 
> > “foo”.object\_id  
> > =\> 1750540
> 
> > :foo.object\_id  
> > =\> 146098
> 
> > “foo”.object\_id  
> > =\> 1734530
> 
> > def foomethod  
> > :foo.object\_id  
> > end  
> > =\> nil
> 
> > foomethod  
> > =\> 146098

Notice how the id of :foo never changes – even inside methods? But  
the id of “foo” does change.

So :foo is cheaper as there is only one in existence in the entire  
program memory.

---

<div class="post-metadata">

**Author:** ![A\_Love\_of\_Surf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@A\_Love\_of\_Surf](https://www.ruby-forum.com/u/A_Love_of_Surf)\
**Post date:** [July 23, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/best-practice-symbol-string-literal/143921/3 "2008-07-23T23:08:25Z")

</div>

Josh S. says Symbols are not pretty strings -  
[http://tinyurl.com/4sqwt9](http://tinyurl.com/4sqwt9)
