When to use :symbol and when to use String?

Hi all

I experience that the use of symbols and strings is not consistent in
Rails.

Very often I can use symbols as values in methods:

has_and_belongs_to_many :musician_profiles,
:join_table => “musician_profiles_do_show_types”

string

has_and_belongs_to_many :musician_profiles,
:join_table => :musician_profiles_do_show_types

symbol

But for example the link_to helper does not allow me to use a symbol
instead of a string:

link_to “Test”, :controller => “news” # string

link_to “Test”, :controller => :news # symbol

This second version using a symbol gives me this error:

undefined method `include?’ for :news:Symbol

Is there any reason for this? And if not, is there a way to achieve its
functionality also with symbols, maybe using a plugin?

Thanks for infos,
Joshua

Dont know about rails, but use symbols whenever you dont
need string behaviour (strnig behaviour like “foobar”[0,1] etc)

There is really not a smart answer to this, at least as it pertains
to Rails, besides that Rails is simply inconsistent on this matter in
many cases. This is mostly for historical reasons.

However, here is a good explanation of Symbols in general. This is a
common question, as you can probably imagine: http://onestepback.org/
index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red

  • Jake McArthur

However, here is a good explanation of Symbols in general. This is a
common question, as you can probably imagine: http://onestepback.org/
index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red

Thank you for posting this. I’ve wondered about this many times, and
this is the best explanation I’ve seen.