Why use a symbol in place of a variable?

Discussion in 'Ruby' started by Gaba Luschi, Feb 16, 2011.

  1. Gaba Luschi

    Gaba Luschi Guest

    Hi,
    Say you have a method in a class that's defined as this:
    (working from Peter Cooper's Beginning Ruby book, p. 155 of the book)

    class Dungeon
     
    Gaba Luschi, Feb 16, 2011
    #1
    1. Advertising

  2. A symbol and a variable are two different things. A symbol is
    essentially a special kind of literal, just like a number or a string
    is. You cannot assign values to symbols, just like you can't assign
    values to numbers or strings -- they are their own values. That is, it
    makes no sense to say `42 = "banana"`, just as it makes no sense to
    say `:banana = 42`.

    In this case, the author is using the symbol :largecave to represent a
    particular location. The reason why he might prefer a symbol literal
    to a string literal is that symbols are immutable. "Immutable" means
    that you can't do operations on symbols to change them (unlike, say,
    strings). Immutability is a good property because it decreases that
    number of surprises you can have, and because it makes reasoning about
    your program easier.

    ~ jf
    --
    John Feminella
    Principal Consultant, BitsBuilder
    LI: http://www.linkedin.com/in/johnxf
    SO: http://stackoverflow.com/users/75170/



    On Tue, Feb 15, 2011 at 19:41, Gaba Luschi <> wrote:
    > Hi,
    > Say you have a method in a class that's defined as this:
    > (working from Peter Cooper's Beginning Ruby book, p. 155 of the book)
    >
    > class Dungeon
    > .
    > .
    > .
    >
    > def start(location)
    > @player.location = location
    > show_current_description
    > end
    >
    > why is it that when you place the player in the large cave, it's
    > my_dungeon.start:)largecave)
    >
    > instead of
    > my_dungeon.start(largecave)
    > ?
    > why is largecave a symbol?
    > Thanks so much!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    John Feminella, Feb 16, 2011
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    This is quite good tutorial about symbols and strings here
    http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/

    On Wed, Feb 16, 2011 at 6:38 AM, John Feminella <>wrote:

    > A symbol and a variable are two different things. A symbol is
    > essentially a special kind of literal, just like a number or a string
    > is. You cannot assign values to symbols, just like you can't assign
    > values to numbers or strings -- they are their own values. That is, it
    > makes no sense to say `42 = "banana"`, just as it makes no sense to
    > say `:banana = 42`.
    >
    > In this case, the author is using the symbol :largecave to represent a
    > particular location. The reason why he might prefer a symbol literal
    > to a string literal is that symbols are immutable. "Immutable" means
    > that you can't do operations on symbols to change them (unlike, say,
    > strings). Immutability is a good property because it decreases that
    > number of surprises you can have, and because it makes reasoning about
    > your program easier.
    >
    > ~ jf
    > --
    > John Feminella
    > Principal Consultant, BitsBuilder
    > LI: http://www.linkedin.com/in/johnxf
    > SO: http://stackoverflow.com/users/75170/
    >
    >
    >
    > On Tue, Feb 15, 2011 at 19:41, Gaba Luschi <>
    > wrote:
    > > Hi,
    > > Say you have a method in a class that's defined as this:
    > > (working from Peter Cooper's Beginning Ruby book, p. 155 of the book)
    > >
    > > class Dungeon
    > > .
    > > .
    > > .
    > >
    > > def start(location)
    > > @player.location = location
    > > show_current_description
    > > end
    > >
    > > why is it that when you place the player in the large cave, it's
    > > my_dungeon.start:)largecave)
    > >
    > > instead of
    > > my_dungeon.start(largecave)
    > > ?
    > > why is largecave a symbol?
    > > Thanks so much!
    > >
    > > --
    > > Posted via http://www.ruby-forum.com/.
    > >
    > >

    >
    >
     
    sanjeev mathur, Feb 16, 2011
    #3
  4. Gaba Luschi

    Chris Kottom Guest

    [Note: parts of this message were removed to make it a legal post.]

    Along with what John Feminella said about symbols' immutability is the
    advantage they have over the string literals that they also often replace,
    and that is the fact that each new instance of a string literal will refer
    to a different Ruby object while each reference to the equivalent symbol
    will refer to the same object instance with the added bonus that less memory
    is used. See the example below.

    ruby-1.9.2-p136 :001 > "hi".object_id
    => 71175750
    ruby-1.9.2-p136 :002 > "hi".object_id
    => 71172980
    ruby-1.9.2-p136 :003 > :hi.object_id
    => 185928
    ruby-1.9.2-p136 :004 > :hi.object_id
    => 185928


    On Wed, Feb 16, 2011 at 5:04 AM, sanjeev mathur <>wrote:

    > This is quite good tutorial about symbols and strings here
    >
    > http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings/
    >
    > On Wed, Feb 16, 2011 at 6:38 AM, John Feminella <
    > >wrote:

    >
    > > A symbol and a variable are two different things. A symbol is
    > > essentially a special kind of literal, just like a number or a string
    > > is. You cannot assign values to symbols, just like you can't assign
    > > values to numbers or strings -- they are their own values. That is, it
    > > makes no sense to say `42 = "banana"`, just as it makes no sense to
    > > say `:banana = 42`.
    > >
    > > In this case, the author is using the symbol :largecave to represent a
    > > particular location. The reason why he might prefer a symbol literal
    > > to a string literal is that symbols are immutable. "Immutable" means
    > > that you can't do operations on symbols to change them (unlike, say,
    > > strings). Immutability is a good property because it decreases that
    > > number of surprises you can have, and because it makes reasoning about
    > > your program easier.
    > >
    > > ~ jf
    > > --
    > > John Feminella
    > > Principal Consultant, BitsBuilder
    > > LI: http://www.linkedin.com/in/johnxf
    > > SO: http://stackoverflow.com/users/75170/
    > >
    > >
    > >
    > > On Tue, Feb 15, 2011 at 19:41, Gaba Luschi <>
    > > wrote:
    > > > Hi,
    > > > Say you have a method in a class that's defined as this:
    > > > (working from Peter Cooper's Beginning Ruby book, p. 155 of the book)
    > > >
    > > > class Dungeon
    > > > .
    > > > .
    > > > .
    > > >
    > > > def start(location)
    > > > @player.location = location
    > > > show_current_description
    > > > end
    > > >
    > > > why is it that when you place the player in the large cave, it's
    > > > my_dungeon.start:)largecave)
    > > >
    > > > instead of
    > > > my_dungeon.start(largecave)
    > > > ?
    > > > why is largecave a symbol?
    > > > Thanks so much!
    > > >
    > > > --
    > > > Posted via http://www.ruby-forum.com/.
    > > >
    > > >

    > >
    > >

    >
     
    Chris Kottom, Feb 16, 2011
    #4
  5. Another way to look at it is a symbol is it is an emnumerated scalar.

    You could have

    cavesize = 345
    my_dungeon.start(cavesize)

    However if caves are only ever Small, Medium and Large, then you can
    represent that finite set of options by passing :smallcave, :mediumcave
    or :largecave.

    A symbol can therefore more closely represent the logic of the problem.

    --
    Posted via http://www.ruby-forum.com/.
     
    Mike Stephens, Feb 16, 2011
    #5
  6. > Another way to look at it is a symbol is it is an emnumerated scalar.

    I don't think that's an accurate characterization, imo, since symbols
    have no relationship or knowledge of other symbols. They are not a
    collection of anything, and thus by definition can't be an
    enumeration. Using your example, nothing stops you from writing

    > my_dungeon.start:)bananas)


    and that would be a perfectly valid parameter to supply. However, if
    you wrote something like

    > my_dungeon.start(DungeonSizes[:bananas])


    that would clue you into the problem. In this case, DungeonSizes would
    be more like the enumeration, into which a symbol would be the index
    for the element of the enumeration you wanted to select.

    ~ jf
    --
    John Feminella
    Principal Consultant, BitsBuilder
    LI: http://www.linkedin.com/in/johnxf
    SO: http://stackoverflow.com/users/75170/



    On Wed, Feb 16, 2011 at 10:34, Mike Stephens <> wrote:
    > Another way to look at it is a symbol is it is an emnumerated scalar.
    >
    > You could have
    >
    > cavesize = 345
    > my_dungeon.start(cavesize)
    >
    > However if caves are only ever Small, Medium and Large, then you can
    > represent that finite set of options by passing :smallcave, :mediumcave
    > or :largecave.
    >
    > A symbol can therefore more closely represent the logic of the problem.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    John Feminella, Feb 16, 2011
    #6
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    907
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,984
    Smokey Grindel
    Dec 2, 2006
  3. baumann@pan
    Replies:
    1
    Views:
    746
    Richard Bos
    Apr 15, 2005
  4. Song Ma
    Replies:
    2
    Views:
    233
    Charles Oliver Nutter
    Jul 20, 2008
  5. Replies:
    6
    Views:
    1,800
Loading...

Share This Page