Symbols vs. Strings

Discussion in 'Ruby' started by John Blanco, May 29, 2007.

  1. John Blanco

    John Blanco Guest

    I'm new to Ruby, but coming up to speed quickly. One question I still
    have never seen a good explanation to is this: When is it preferred to a
    key a hash with a symbol, and when is it keyed by string? Is this just
    personal preference, or is there a rule of thumb?

    For example, in the Rails book, the session variable is always populated
    with symbols, i.e.:

    session[:user] = User.new

    It's also obviously completely common throughout the Rails framework
    (e.g., :controller =>, :action =>, etc.)

    So, when should I use what...or what should I prefer?

    --
    Posted via http://www.ruby-forum.com/.
    John Blanco, May 29, 2007
    #1
    1. Advertising

  2. John Blanco

    Tim Hunter Guest

    John Blanco wrote:
    > I'm new to Ruby, but coming up to speed quickly. One question I still
    > have never seen a good explanation to is this: When is it preferred to a
    > key a hash with a symbol, and when is it keyed by string? Is this just
    > personal preference, or is there a rule of thumb?
    >
    > For example, in the Rails book, the session variable is always populated
    > with symbols, i.e.:
    >
    > session[:user] = User.new
    >
    > It's also obviously completely common throughout the Rails framework
    > (e.g., :controller =>, :action =>, etc.)
    >
    > So, when should I use what...or what should I prefer?


    This question comes up a lot. You can search the list archives for
    comprehensive discussions. Try
    http://groups.google.com/group/comp...gst&q=symbols strings&rnum=3#e495f6051aebe0ae

    --
    Posted via http://www.ruby-forum.com/.
    Tim Hunter, May 29, 2007
    #2
    1. Advertising

  3. John Blanco

    Emilio Tagua Guest

    On 5/29/07, John Blanco <> wrote:
    > I'm new to Ruby, but coming up to speed quickly. One question I still
    > have never seen a good explanation to is this: When is it preferred to a
    > key a hash with a symbol, and when is it keyed by string? Is this just
    > personal preference, or is there a rule of thumb?
    >
    > For example, in the Rails book, the session variable is always populated
    > with symbols, i.e.:
    >
    > session[:user] = User.new
    >
    > It's also obviously completely common throughout the Rails framework
    > (e.g., :controller =>, :action =>, etc.)
    >
    > So, when should I use what...or what should I prefer?


    Symbols are ligh-weight, they don't have so much methods to initialize
    has strings:

    irb(main):009:0> :asd.methods.size
    => 45
    irb(main):010:0> "asd".methods.size
    => 143

    So thats why you use it in params or other places when you just need
    the name. Also a difference is that a symbol :sym is :sym everywhere,
    but a string "string" -only the string, not assigned to a variable- is
    a different refence to a String object each time you write "string" .

    Those are the most important diferences IMO. Sorry if i wasn't that clear tho.

    Cheers
    Emilio Tagua, May 29, 2007
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi,

    One thing to keep in mind is that, as far as I know, symbols are
    never garbage collected, so their excessive/inappropriate use might
    introduce risk of memory leaks.

    George

    On 30 May 2007, at 00:06, Dave Grijalva wrote:

    > I think the second point that Emilio was making is the more
    > important one.
    > Let me try to clarify:
    >
    > irb(main):008:0> puts "foo".object_id
    > 1724824
    > => nil
    > irb(main):009:0> puts "foo".object_id
    > 1708744
    > => nil
    > irb(main):010:0> puts :foo.object_id
    > 3678478
    > => nil
    > irb(main):011:0> puts :foo.object_id
    > 3678478
    >
    >
    > You can see here that a new String object is created every time you
    > use a
    > literal string. With a symbol, however, you are re-using the same
    > object
    > anywhere you use a symbol with the same name. Using symbols can
    > save you a
    > lot of resources, both processor time and memory.
    >
    > -dave
    >
    > On 5/29/07, Emilio Tagua <> wrote:
    >>
    >> On 5/29/07, John Blanco <> wrote:
    >> > I'm new to Ruby, but coming up to speed quickly. One question I

    >> still
    >> > have never seen a good explanation to is this: When is it

    >> preferred to a
    >> > key a hash with a symbol, and when is it keyed by string? Is

    >> this just
    >> > personal preference, or is there a rule of thumb?
    >> >
    >> > For example, in the Rails book, the session variable is always

    >> populated
    >> > with symbols, i.e.:
    >> >
    >> > session[:user] = User.new
    >> >
    >> > It's also obviously completely common throughout the Rails

    >> framework
    >> > (e.g., :controller =>, :action =>, etc.)
    >> >
    >> > So, when should I use what...or what should I prefer?

    >>
    >> Symbols are ligh-weight, they don't have so much methods to
    >> initialize
    >> has strings:
    >>
    >> irb(main):009:0> :asd.methods.size
    >> => 45
    >> irb(main):010:0> "asd".methods.size
    >> => 143
    >>
    >> So thats why you use it in params or other places when you just need
    >> the name. Also a difference is that a symbol :sym is :sym everywhere,
    >> but a string "string" -only the string, not assigned to a
    >> variable- is
    >> a different refence to a String object each time you write
    >> "string" .
    >>
    >> Those are the most important diferences IMO. Sorry if i wasn't
    >> that clear
    >> tho.
    >>
    >> Cheers
    >>
    >>


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (Darwin)

    iD8DBQFGXTZcuHSlGoVGf7URAqhKAKC28rsjkJ7tv2f1rxsdidLachrB6QCgs50D
    qqsnSiEX19rHfPqzA9z9/DI=
    =tKg1
    -----END PGP SIGNATURE-----
    George Malamidis, May 30, 2007
    #4
    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:
    757
    Malcolm
    Jun 24, 2006
  2. Daniel Berger
    Replies:
    2
    Views:
    426
    Park Heesob
    Nov 28, 2003
  3. Joe Van Dyk

    symbols vs strings vs ?

    Joe Van Dyk, Feb 3, 2005, in forum: Ruby
    Replies:
    2
    Views:
    87
    Joe Van Dyk
    Feb 3, 2005
  4. Mark Volkmann

    symbols vs. strings

    Mark Volkmann, Aug 26, 2005, in forum: Ruby
    Replies:
    0
    Views:
    97
    Mark Volkmann
    Aug 26, 2005
  5. Wes Gamble
    Replies:
    8
    Views:
    155
    Sean O'Halpin
    Jul 23, 2006
Loading...

Share This Page