More on Symbols

From what I have read, in Ruby a Symbol is prefixed with a colon as
in :my_symbol, yet I can do this…

:my_symbol
=> :my_symbol

my_variable = :my_symbol
=> :my_symbol

:my_symbol.class
=> Symbol

:my_symbol.object_id
=> 199218

my_variable.class
=> Symbol

my_variable.object_id
=> 199218

Comments?

I think of symbols as similar to numbers. How about:

1
=> 1

my_variable = 1
=> 1

1.class
=> Fixnum

1.object_id
=> 3

my_variable.class
=> Fixnum

my_variable.object_id
=> 3

Is it Symbols that you’re questioning, or is it that :my_symbol and
my_variable have the same object_id?

They have the same object_id because they’re pointing at the same
object.

Bryan

what you’ve done in this case is have a variable that holds and points
back
to the symbol. in a sense that variable is a symbol but the symbol it
refers
to had to be created with the normal Ruby syntax. here’s a good link
that
might help you understand all that complexities of how symbols work in
Ruby:
http://www.troubleshooters.com/codecorn/ruby/symbols.htm Beware it’s a
long
read. :slight_smile:

hope that helps.

RSL

On Jul 15, 8:29 pm, Bryan A. [email protected] wrote:

They have the same object_id because they’re pointing at the same
object.

Agreed.

I was trying to demonstrate that a variable, say the unassuming x,
could actually turn out to be a Symbol (even though it is not prefixed
with a colon). However, that is not surprising because:

  1. In Ruby one can write

x = :my_symbol

but can’t write

:my_symbol = x # This is sort of equivalent to trying to write 5 = x

That is, apart from specifying a Symbol literally (e.g. :my_symbol)
one can only specify a Symbol by specifying the variable that
references it. Like ALL objects in Ruby, to specify the object you
either specify it literally or specify the variable that references
it.

  1. Since Ruby is a dynamically typed language a variable, say the
    unassuming x again, can reference different classes of objects
    throughout a program. As such, a variable could reference any class
    of object at a particular moment in time, including a Symbol object.

Apologies if this is not a revelation to the reader.

On Jul 15, 9:48 pm, RSL [email protected] wrote:

here’s a good link that
might help you understand all that complexities of how symbols work in Ruby:http://www.troubleshooters.com/codecorn/ruby/symbols.htmBeware it’s a long
read. :slight_smile:

Thanks. Have already read that link; it is very good.

On Jul 16, 12:11 am, Rick DeNatale [email protected] wrote:

This article might help

http://talklikeaduck.denhaven2.com/2006/09/13/

I think my understanding is correct and inline with the link you
gave. However the link’s author did make some important points:

  1. Besides the Symbol class, other classes in Ruby such as Fixnum,
    NilClass, TrueClass and FalseClass “ensure that if ‘two’ of their
    instances are equal they are the same object.”

  2. Ruby’s freeze method makes a particular instance immutable; adding
    “Beware, though, that this only freezes the object itself and not it’s
    sub-objects, or referenced objects”

On Wed, Jul 15, 2009 at 2:21 AM, shusseina[email protected]
wrote:

:my_symbol.object_id
=> 199218

my_variable.class
=> Symbol

my_variable.object_id
=> 199218

Comments?

A common Ruby beginner confusion is not understanding the relationship
between objects and variables.

This article might help

http://talklikeaduck.denhaven2.com/2006/09/13/


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs