Symbols vs strings in Rails


#1

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).


#2

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.


#3

On 11/28/05, Rodrigo A. 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

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 :slight_smile:


rick
http://techno-weenie.net


#4

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 Reality won’t budge
http://www.schuerig.de/michael/ --Rush, Show Don’t Tell


#5

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


#6

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