When to use :symbol and when to use String?

J

Joshua Muheim

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

Marc Heiler

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

Jake McArthur

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
 
G

Giles Bowkett

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top