Forum: Ruby what are sympbols?!?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain (Guest)
on 2006-04-05 19:14
Hi, simple question here but I cant figure it out after reading the Ruby
book.

How symbols work? I come from a Java background and I cant understand
what symbols are and how to work with them. I cant figure out when I am
supposed to use them in a function. Seems to me that the description
from the book is too short for such a concept.

Please, explain!

Thanks!
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-05 19:32
(Received via mailing list)
On Apr 5, 2006, at 1:14 PM, Alain wrote:

> Please, explain!
>
> Thanks!
>
> --
> Posted via http://www.ruby-forum.com/.
>

Well you can search the archives, this question has been asked many
times. But I can take a stab at it, I like to think of symbols as
names. Names that can be used in various places, for instance, using
the send method, it wants to know the name of the method to send, so
you use a symbol

"Hello".send(:display)

Or if you are using a hash, you can use symbols as keys to name the
elements:

{ :height => 27, :width => 34, :color => "green" }

There is only one symbol for every name, a symbol :x is not the
object named x, it is the name, "x".

:Logan is not Logan (me), it's the name "Logan"
Many people can have the same name but different objects. Two people
can be called Joe, but they still have the same name "Joe".

Likewise in ruby:

{ :class => "A"  }

1.send(:class)

The hash key has the same name as the method being sent via send, but
the results are different. A symbol doesn't point, it is just the
name. I point because one of the advantages of symbols is that they
are fast:

:name == :name is just as quick as comparing a number. People use
this detail all the time in their code. They need to give something a
name, a name that the user will probably never see (like a key in a
hash, or the argument to a method, like say draw_at(0,0, :circle). )
The name is easier to remember than any other arbitrary object (often
times a number will be used or an enum in other languages). So in
short IMO, a symbol is a name.
A0eee794e60f2555d410b8accb674ffb?d=identicon&s=25 Les Nightingill (lazylester)
on 2006-04-07 15:20
Logan Capaldo wrote:
> > Likewise in ruby:
>
> { :class => "A"  }
>
> 1.send(:class)
>
> The hash key has the same name as the method being sent via send, but
> the results are different.

I too am struggling to understand, and this statement has me even more
confused!

How can my results be predictable if :class isn't uniquely associated
with something (hash key, or method)? Doesn't this lead to ambiguities?
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-07 15:56
(Received via mailing list)
On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:

> How can my results be predictable if :class isn't uniquely associated
> with something (hash key, or method)? Doesn't this lead to ambiguities?

hth.

C:\Documents and Settings\Gregory Brown>irb
irb(main):001:0> :class.object_id
=> 788750
irb(main):002:0> :apple.object_id
=> 6119694
irb(main):003:0>
52a177e9dbd3e614825aabc4e45f8cd6?d=identicon&s=25 Mark Volkmann (Guest)
on 2006-04-07 16:02
(Received via mailing list)
On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:
> I too am struggling to understand, and this statement has me even more
> confused!
>
> How can my results be predictable if :class isn't uniquely associated
> with something (hash key, or method)? Doesn't this lead to ambiguities?

Questions about symbols come up about every two weeks. You can find
lots of good explanations in the archives. Here's a really brief one.

Think of symbols as strings whose value never changes and where there
are never two of them with the same value. Every time I use :foo in my
code, it refers to the same Symbol object. Using symbols instead of
strings saves space and it's faster to compare two symbols that to
compare two strings since only their address in memory needs to be
compared, not their individual characters.

Some of the most common uses of symbols are
- parameters to attr_accessor, attr_reader and attr_writer for
generating corresponding get and set methods
- keys in Hashes
- method identifiers used with the Object send method
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-04-07 17:31
(Received via mailing list)
On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:
> I too am struggling to understand, and this statement has me even more
> confused!
>
> How can my results be predictable if :class isn't uniquely associated
> with something (hash key, or method)? Doesn't this lead to ambiguities?

No. Like most people, you seem to have assumed that Symbols are
special. They aren't. Really, there's not much difference between:

  { :class => "A" }

and

  { 5 => "A" }

It's the *methods* that are special. Not the Symbols. Symbols are just
invariant names. The only *really* special thing about them is that
syntax automatically creates Symbol objects, and those Symbol objects
exist for the life of the program. That is,

  def foo
  end

creates a Symbol called :foo. (If one doesn't already exist.) Similarly,

  class Foo
  end

creates a Symbol called :Foo (again, if one doesn't already exist).

But the real magic of a Symbol isn't in the Symbol; it's just a name.
There's nothing special or magical about the name "Les Nightingill" --
except that it identifies you ... and anyone else named "Les
Nightingill." There might be very few people named "Les Nightingill",
but I know of at least three people named "Austin Ziegler" (including
myself) and many more named "Austin". It's how you *use* the name
involved that makes the name special.

So the magic behind #attr_acessor and #send (or #__send__) is the
method, not its Symbol argument.

Sort of like "Santorum." ;)

-austin
5f3d7eb9e9314f4eb94b4040cee4d8d2?d=identicon&s=25 Vince Puzzella (Guest)
on 2006-04-07 17:44
(Received via mailing list)
Hi folks,

I just started getting this error all of a sudden (command line
snippet):

vmac:~/workspace/eir vince$ ruby -v
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
vmac:~/workspace/eir vince$ rails -v
Rails 1.0.0
vmac:~/workspace/eir vince$ gem -v
0.8.11
vmac:~/workspace/eir vince$ ./script/server
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require__': No
such file to load -- initializer (LoadError)
        from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
        from ./script/../config/boot.rb:16
        from ./script/server:2:in `require'
        from ./script/server:2
vmac:~/workspace/eir vince$

Is anybody else having the same issue?  Any clues as to what may be
wrong?
I did nothing to my environment, but the problem started after Mac OS X
notified me that updates were available and I started them in the
back-ground.

On 4/7/06 11:30 AM, "Austin Ziegler" <halostatue@gmail.com> wrote:

> > The hash key has the same name as the method being sent
> (hash key, or method)? Doesn't this lead to ambiguities?
No. Like most
> people, you seem to have assumed that Symbols are
special. They aren't.
> Really, there's not much difference between:

  { :class => "A" }

and

  { 5
> => "A" }

It's the *methods* that are special. Not the Symbols. Symbols are
> just
invariant names. The only *really* special thing about them is
> that
syntax automatically creates Symbol objects, and those Symbol
> objects
exist for the life of the program. That is,

  def foo
  end

creates
> a Symbol called :foo. (If one doesn't already exist.) Similarly,

  class Foo

> end

creates a Symbol called :Foo (again, if one doesn't already exist).

But
> the real magic of a Symbol isn't in the Symbol; it's just a name.
There's
> nothing special or magical about the name "Les Nightingill" --
except that it
> identifies you ... and anyone else named "Les
Nightingill." There might be
> very few people named "Les Nightingill",
but I know of at least three people
> named "Austin Ziegler" (including
myself) and many more named "Austin". It's
> how you *use* the name
involved that makes the name special.

So the magic
> behind #attr_acessor and #send (or #__send__) is the
method, not its Symbol
> argument.

Sort of like "Santorum." ;)

-austin
5f3d7eb9e9314f4eb94b4040cee4d8d2?d=identicon&s=25 Vince Puzzella (Guest)
on 2006-04-07 17:50
(Received via mailing list)
Oops!  Sorry, wrong list!
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2006-04-07 18:33
(Received via mailing list)
[ snip ]


> but I know of at least three people
> > named "Austin Ziegler" (including
> myself)



ahh so it is not you?




--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein
Efe70cff891d5722448a6512cfd1e1f1?d=identicon&s=25 David Bailey (dbailey7)
on 2006-04-07 21:35
Alain wrote:
> Hi, simple question here but I cant figure it out after reading the Ruby
> book.
>
> How symbols work? I come from a Java background and I cant understand
> what symbols are and how to work with them. I cant figure out when I am
> supposed to use them in a function. Seems to me that the description
> from the book is too short for such a concept.
>
> Please, explain!
>
> Thanks!

These

These two posts from the Ruby forum and four links should answer
virtually all of your questions about Ruby symbols:

"deciphering poignant guide chapter 4 example -
Posted by unknown (Guest) on 26.03.2006 09:19"

"Getting Over Symbols - Posted by gwtmp01 (Guest) on 04.12.2005 20:11"

http://www.rubygarden.org/ruby?UnderstandingSymbols

http://glu.ttono.us/articles/2005/08/19/understand...

http://www.oreillynet.com/ruby/blog/2005/12/symbol...

http://www.oreillynet.com/ruby/blog/2005/12/diggin...


P.S. The Ruby Garden link came out of the Poignant Chapter 4 Example
post, so if you have limited time, start with these two and read the
rest as time permits.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-08 00:12
(Received via mailing list)
On Apr 7, 2006, at 11:30 AM, Austin Ziegler wrote:

> But the real magic of a Symbol isn't in the Symbol; it's just a name.
> There's nothing special or magical about the name "Les Nightingill" --
> except that it identifies you ... and anyone else named "Les
> Nightingill." There might be very few people named "Les Nightingill",
> but I know of at least three people named "Austin Ziegler" (including
> myself) and many more named "Austin". It's how you *use* the name
> involved that makes the name special.

Thank you, this is a much clearer explanation of the point I was
trying to get across.
This topic is locked and can not be replied to.