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. Advertising

  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. Advertising

  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. Advertising

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:
    732
    Malcolm
    Jun 24, 2006
  2. Daniel Berger
    Replies:
    2
    Views:
    418
    Park Heesob
    Nov 28, 2003
  3. Mark Volkmann

    symbols vs. strings

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

    Symbols vs Strings

    matt, Dec 21, 2006, in forum: Ruby
    Replies:
    4
    Views:
    189
    Jeremy Wells
    Dec 21, 2006
Loading...

Share This Page