Best-practice: :symbol || 'string_literal'?

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

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs