# Symbols vs strings in Rails

**URL:** <https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833>\
**Category:** Rails\
**Created:** [November 25, 2005, 2:57pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833 "2005-11-25T14:57:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [November 25, 2005, 2:57pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/1 "2005-11-25T14:57:57Z")

</div>

I’ve seen a few inconsistencies both in AWDR and the rails API docs, but  
the de facto standard is:

:controller =\> ‘foo’, :action =\> ‘bar’

I wonder what the reason is for supplying foo and bar as strings instead  
of symbols? One thing I can think of is that with many arguments you  
probably increase the readability compared to:

:controller =\> :bar, :action =\> :bar

I also wonder if any of the benchmark lovers have run tests on double vs  
single quotes? I remember from back when I coded PHP that you could get  
a noticable loss in performance from using double quotes (when not  
necessary of course).

---

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [November 28, 2005, 5:35pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/2 "2005-11-28T17:35:42Z")

</div>

greendale wrote:

> I’ve seen a few inconsistencies both in AWDR and the rails API docs, but  
> the de facto standard is:
> 
> :controller =\> ‘foo’, :action =\> ‘bar’
> 
> I wonder what the reason is for supplying foo and bar as strings instead  
> of symbols? One thing I can think of is that with many arguments you  
> probably increase the readability compared to:
> 
> :controller =\> :bar, :action =\> :bar
> 
> I also wonder if any of the benchmark lovers have run tests on double vs  
> single quotes? I remember from back when I coded PHP that you could get  
> a noticable loss in performance from using double quotes (when not  
> necessary of course).

I’m really interested in this topic too, since i come from PHP, and  
there i use apostrophes when possible.

---

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [November 28, 2005, 5:43pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/3 "2005-11-28T17:43:07Z")

</div>

On 11/28/05, Rodrigo A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > :controller =\> :bar, :action =\> :bar
> > 
> > I also wonder if any of the benchmark lovers have run tests on double vs  
> > single quotes? I remember from back when I coded PHP that you could get  
> > a noticable loss in performance from using double quotes (when not  
> > necessary of course).
> 
> I’m really interested in this topic too, since i come from PHP, and  
> there i use apostrophes when possible.

Symbols are efficient strings. Anytime you use ‘blah’ it creates a  
new string in memory. But, every copy of :blah is exactly the same.

Kevin has a great article on this topic:  
[http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols](http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols)

I believe that you can pass the action as a symbol, such as :action =\>  
:bar, but not controllers. There is the possibility of controllers  
having a path, such as :controller =\> ‘admin/foo’ or :controller =\>  
‘/account’.

The real reason I use symbols is it’s 1 less character to type, and  
textmate gives it this pretty color 🙂

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [November 28, 2005, 7:08pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/4 "2005-11-28T19:08:12Z")

</div>

On Monday 28 November 2005 17:42, Rick O. wrote:

> Symbols are efficient strings. Anytime you use ‘blah’ it creates a  
> new string in memory. But, every copy of :blah is exactly the same.

This is only true when symbols are used consistently. In Rails there are  
numerous places where conversion from strings to symbols or the other  
way around occur. Just search for stringify\_keys and symbolize\_keys.  
The same goes for Hash#with\_indifferent\_access.

Therefore, I’m inclined to doubt that the use of symbols in its current  
state in Rails improves efficiency.

Michael

–  
Michael S. You can twist perceptions  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) Reality won’t budge  
[Michael Schürig | Sentenced to making sense](http://www.schuerig.de/michael/) --Rush, Show Don’t Tell

---

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [December 3, 2005, 11:03pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/5 "2005-12-03T23:03:30Z")

</div>

Rodrigo A. Fernández wrote:

> Thanks. What about the double quotes vs. apostrophes? Do quotes increase  
> memory usage or app performance?

I did a few very simple tests with the profiler, just looping through  
some string operations 100000 times, and I couldn’t find any difference  
at all in performance between single and double quotes.

I did however learn that

x = “#{str1}#{str2}”

is about four times faster than

x = str1 + str2

---

<div class="post-metadata">

**Author:** ![greendale](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/53a042/32.png) [@greendale](https://www.ruby-forum.com/u/greendale)\
**Post date:** [November 28, 2005, 6:27pm UTC](https://www.ruby-forum.com/t/symbols-vs-strings-in-rails/48833/6 "2005-11-28T18:27:35Z")

</div>

Thanks. What about the double quotes vs. apostrophes? Do quotes increase  
memory usage or app performance?
