Google Groups Home
Help | Sign in
symbols vs strings vs ?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Joe Van Dyk  
View profile  
 More options Feb 2 2005, 7:26 pm
Newsgroups: comp.lang.ruby
From: Joe Van Dyk <joevan...@gmail.com>
Date: Thu, 3 Feb 2005 09:26:58 +0900
Local: Wed, Feb 2 2005 7:26 pm
Subject: symbols vs strings vs ?
Any rules or guidelines on when to use symbols vs strings?  I'm not
sure as to the advantages of using symbols.

Thanks,
Joe


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Assaph Mehr  
View profile  
 More options Feb 2 2005, 7:33 pm
Newsgroups: comp.lang.ruby
From: "Assaph Mehr" <ass...@gmail.com>
Date: 2 Feb 2005 16:33:00 -0800
Subject: Re: symbols vs strings vs ?

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joe Van Dyk  
View profile  
 More options Feb 2 2005, 7:44 pm
Newsgroups: comp.lang.ruby
From: Joe Van Dyk <joevan...@gmail.com>
Date: Thu, 3 Feb 2005 09:44:05 +0900
Local: Wed, Feb 2 2005 7:44 pm
Subject: Re: symbols vs strings vs ?

It did help!  Thanks.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
E S  
View profile  
 More options Feb 2 2005, 8:53 pm
Newsgroups: comp.lang.ruby
From: E S <eero.saynatk...@kolumbus.fi>
Date: Thu, 3 Feb 2005 10:53:32 +0900
Local: Wed, Feb 2 2005 8:53 pm
Subject: 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile  
 More options Feb 2 2005, 9:40 pm
Newsgroups: comp.lang.ruby
From: James Edward Gray II <ja...@grayproductions.net>
Date: Thu, 3 Feb 2005 11:40:34 +0900
Local: Wed, Feb 2 2005 9:40 pm
Subject: Re: symbols vs strings vs ?
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
E S  
View profile  
 More options Feb 2 2005, 9:50 pm
Newsgroups: comp.lang.ruby
From: E S <eero.saynatk...@kolumbus.fi>
Date: Thu, 3 Feb 2005 11:50:26 +0900
Local: Wed, Feb 2 2005 9:50 pm
Subject: Re: symbols vs strings vs ?

> Lähettäjä: James Edward Gray II <ja...@grayproductions.net>
> Aihe: Re: symbols vs strings vs ?

> 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?

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile  
 More options Feb 2 2005, 11:01 pm
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@wobblini.net>
Date: Thu, 3 Feb 2005 13:01:36 +0900
Local: Wed, Feb 2 2005 11:01 pm
Subject: Re: symbols vs strings vs ?

[ multipart_mixed_part < 1K ]
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

Hi --

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 = file_handle.gets.intern
   if /xyz/.match(sym.to_s) ...

David

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David A. Black  
View profile  
 More options Feb 2 2005, 11:11 pm
Newsgroups: comp.lang.ruby
From: "David A. Black" <dbl...@wobblini.net>
Date: Thu, 3 Feb 2005 13:11:54 +0900
Local: Wed, Feb 2 2005 11:11 pm
Subject: Re: symbols vs strings vs ?
Hello again --

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Weirich  
View profile  
 More options Feb 2 2005, 11:22 pm
Newsgroups: comp.lang.ruby
From: Jim Weirich <j...@weirichhouse.org>
Date: Thu, 3 Feb 2005 13:22:24 +0900
Local: Wed, Feb 2 2005 11:22 pm
Subject: Re: symbols vs strings vs ?
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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Austin Ziegler  
View profile  
 More options Feb 3 2005, 10:44 am
Newsgroups: comp.lang.ruby
From: Austin Ziegler <halosta...@gmail.com>
Date: Fri, 4 Feb 2005 00:44:05 +0900
Local: Thurs, Feb 3 2005 10:44 am
Subject: Re: symbols vs strings vs ?

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 * halosta...@gmail.com
               * Alternate: aus...@halostatue.ca


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google