[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

When to use :symbol and when to use String?

Joshua Muheim

8/20/2006 11:10:00 AM

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

--
Posted via http://www.ruby-....

3 Answers

Marc Heiler

8/20/2006 12:14:00 PM

0

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

--
Posted via http://www.ruby-....

Jake McArthur

8/20/2006 1:41:00 PM

0

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://oneste...
index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red

- Jake McArthur

On Aug 20, 2006, at 6:09 AM, Joshua Muheim wrote:

> 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
>
> --
> Posted via http://www.ruby-....
>


Giles Bowkett

8/21/2006 1:08:00 AM

0

> However, here is a good explanation of Symbols in general. This is a
> common question, as you can probably imagine: http://oneste...
> 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.

--
Giles Bowkett
http://www.gilesg...