What are sympbols?!?

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!

···

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

···

On Apr 5, 2006, at 1:14 PM, 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!

--
Posted via http://www.ruby-forum.com/\.

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://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols

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.

···

--
Posted via http://www.ruby-forum.com/\.

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?

···

--
Posted via http://www.ruby-forum.com/\.

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>

···

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?

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

···

On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:

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?

--
R. Mark Volkmann
Object Computing, Inc.

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." :wink:

-austin

···

On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:

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?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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.

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?

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." :wink:

-austin

···

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

On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:

--
Austin Ziegler *

halostatue@gmail.com

               * Alternate: austin@halostatue.ca

Thank you, this is a much clearer explanation of the point I was trying to get across.

···

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.

Oops! Sorry, wrong list!

···

On 4/7/06 11:43 AM, "Vince Puzzella" <vpuz@rogers.com> wrote:

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:

On 4/7/06, Les Nightingill <lester@ix.netcom.com> wrote:
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?

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." :wink:

-austin
--
Austin Ziegler *

halostatue@gmail.com

               * Alternate: austin@halostatue.ca

[ 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