Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

symbols vs strings vs ?

1 view
Skip to first unread message

Joe Van Dyk

unread,
Feb 2, 2005, 7:26:58 PM2/2/05
to
Any rules or guidelines on when to use symbols vs strings? I'm not
sure as to the advantages of using symbols.

Thanks,
Joe


Assaph Mehr

unread,
Feb 2, 2005, 7:33:00 PM2/2/05
to

Joe Van Dyk wrote:
> Any rules or guidelines on when to use symbols vs strings? I'm not
> sure as to the advantages of using symbols.

Symbols are immutable strings. Every occurence of the same symbol
correspondes to the same single object, while every occurence of the
same string is a different object (with the same value). Thus symbols
are a bit faster and cheaper to use in things like case statements,
hash keys etc.
It's also usually a bit nicer to read in the code, as it signifies that
what you're looking it at is a unique identifier, rather than something
that can have a dynamic content.

HTH,
Assaph

Joe Van Dyk

unread,
Feb 2, 2005, 7:44:05 PM2/2/05
to

It did help! Thanks.


E S

unread,
Feb 2, 2005, 8:53:32 PM2/2/05
to
> Lähettäjä: Joe Van Dyk <joev...@gmail.com>
> Aihe: Re: symbols vs strings vs ?

Always use a Symbol rather than a String, except if you
need to be able to print the string to file/screen/etc.

E

James Edward Gray II

unread,
Feb 2, 2005, 9:40:34 PM2/2/05
to
On Feb 2, 2005, at 7:53 PM, E S wrote:

> Always use a Symbol rather than a String, except if you
> need to be able to print the string to file/screen/etc.

Hmm, don't think I agree with that. What it you need to modify its
contents? What if you want to use some of String's many helper
methods?

James Edward Gray II

E S

unread,
Feb 2, 2005, 9:50:26 PM2/2/05
to
> Lähettäjä: James Edward Gray II <ja...@grayproductions.net>

> Aihe: Re: symbols vs strings vs ?
>

The way I thought of it is that if you need to modify a string,
it's a string that is going to displayed somehow at some point.

For the sake of disambiguity, however, let's amend that to
"Always use a Symbol rather than a constant String..."

> James Edward Gray II

E

David A. Black

unread,
Feb 2, 2005, 11:01:36 PM2/2/05
to
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

David A. Black

unread,
Feb 2, 2005, 11:11:54 PM2/2/05
to
Hello again --

I wrote:

> I don't think this rigid a distinction really works out in practice.
> For example, let's say you read a string from a file, and then do some
> match operations on it. They don't modify the string (it's constant),
> but it's a bit roundabout to do:
>
> sym =3D file_handle.gets.intern
> if /xyz/.match(sym.to_s) ...

I think I misunderstood you. You meant, I now think, things like:

str = "a constant string"

I'm still not convinced that there's any reason to favor having such
things be symbols. Tiny differences in speed (probably almost
literally undetectable except in loops) are worth avoiding a lot of
:"..." or "...".intern/to_sym calls, I think.


David

--
David A. Black
dbl...@wobblini.net


Jim Weirich

unread,
Feb 2, 2005, 11:22:24 PM2/2/05
to
On Wednesday 02 February 2005 11:11 pm, David A. Black wrote:

> I'm still not convinced that there's any reason to favor having such
> things be symbols.

My rule of thumb:

* Use strings when content matters
* Use symbols when identity matters.

--
-- Jim Weirich j...@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)


Austin Ziegler

unread,
Feb 3, 2005, 10:44:05 AM2/3/05
to
On Thu, 3 Feb 2005 13:22:24 +0900, Jim Weirich <j...@weirichhouse.org> wrote:
> On Wednesday 02 February 2005 11:11 pm, David A. Black wrote:
>
> > I'm still not convinced that there's any reason to favor having such
> > things be symbols.
>
> My rule of thumb:
>
> * Use strings when content matters
> * Use symbols when identity matters.

..and that matches mine, too.

-austin
--
Austin Ziegler * halos...@gmail.com
* Alternate: aus...@halostatue.ca


0 new messages