symbols vs strings vs ?

Discussion in 'Ruby' started by Joe Van Dyk, Feb 3, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Any rules or guidelines on when to use symbols vs strings? I'm not
    sure as to the advantages of using symbols.

    Thanks,
    Joe
     
    Joe Van Dyk, Feb 3, 2005
    #1
    1. Advertisements

  2. Joe Van Dyk

    Assaph Mehr Guest

    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
     
    Assaph Mehr, Feb 3, 2005
    #2
    1. Advertisements

  3. Joe Van Dyk

    Joe Van Dyk Guest

    On Thu, 3 Feb 2005 09:35:41 +0900, Assaph Mehr <> wrote:
    >
    > 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


    It did help! Thanks.
     
    Joe Van Dyk, Feb 3, 2005
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,095
    Malcolm
    Jun 24, 2006
  2. Daniel Berger
    Replies:
    2
    Views:
    514
    Park Heesob
    Nov 28, 2003
  3. Mark Volkmann

    symbols vs. strings

    Mark Volkmann, Aug 26, 2005, in forum: Ruby
    Replies:
    0
    Views:
    151
    Mark Volkmann
    Aug 26, 2005
  4. Wes Gamble
    Replies:
    8
    Views:
    264
    Sean O'Halpin
    Jul 23, 2006
  5. matt

    Symbols vs Strings

    matt, Dec 21, 2006, in forum: Ruby
    Replies:
    4
    Views:
    301
    Jeremy Wells
    Dec 21, 2006
  6. John Blanco

    Symbols vs. Strings

    John Blanco, May 29, 2007, in forum: Ruby
    Replies:
    3
    Views:
    184
    George Malamidis
    May 30, 2007
  7. maghac

    Symbols and strings

    maghac, Aug 14, 2007, in forum: Ruby
    Replies:
    6
    Views:
    236
  8. Brian Candler

    Symbols and frozen strings

    Brian Candler, Sep 6, 2007, in forum: Ruby
    Replies:
    12
    Views:
    318
    Daniel DeLorme
    Sep 7, 2007
Loading...