Forum: Ruby on Rails Symbols vs strings in Rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F5a01b20381a7856c15253e2d5f547a5?d=identicon&s=25 Per Melin (greendale)
on 2005-11-25 14:57
(Received via mailing list)
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).
3bec49196b296ac7c201412642c7a2b1?d=identicon&s=25 Rodrigo Alvarez (Guest)
on 2005-11-28 17:35
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.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-11-28 17:43
(Received via mailing list)
On 11/28/05, Rodrigo Alvarez <papipo@gmail.com> 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/understand...

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
3bec49196b296ac7c201412642c7a2b1?d=identicon&s=25 papipo (Guest)
on 2005-11-28 18:27
(Received via mailing list)
Thanks. What about the double quotes vs. apostrophes? Do quotes increase
memory usage or app performance?
Bce1d1b7c3ec7b577dcb42e254899e6b?d=identicon&s=25 michael (Guest)
on 2005-11-28 19:08
(Received via mailing list)
On Monday 28 November 2005 17:42, Rick Olson 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 Schuerig                            You can twist perceptions
mailto:michael@schuerig.de                        Reality won't budge
http://www.schuerig.de/michael/               --Rush, Show Don't Tell
F5a01b20381a7856c15253e2d5f547a5?d=identicon&s=25 Per Melin (greendale)
on 2005-12-03 23:03
(Received via mailing list)
Rodrigo Alvarez 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
This topic is locked and can not be replied to.