How are Symbol objects used

Just saw this over at http://www.c2.com/cgi/wiki?IwannaLearnRuby and
realised I don’t know the answer.

How are Symbol objects used?
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Symbol.html
Why should I use them to create class attributes? --GirtsKalnins

It appears to me that everywhere that you could use a symbol, you could
just as easily use a string. Is there any reason other than aesthetics
that you shouldn’t just use Strings everywhere? Are Symbols just a
hangover from an older Ruby version.

Daniel Sheppard
http://jroller.net/page/soxbox

···

#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

Daniel Sheppard wrote:

It appears to me that everywhere that you could use a symbol, you could
just as easily use a string. Is there any reason other than aesthetics
that you shouldn’t just use Strings everywhere? Are Symbols just a
hangover from an older Ruby version.

A couple of reasons are …

  1. A symbol with a given name is unique. So, if you refer to :hello at 10 places in your code they will all be references to the same object. However, if you had “hello” in those 10 places, you’d have 10 different copies of the string “hello”. In practice, this probably isn’t a big deal.

  2. Comparison of symbols is very cheap, since it can just compare the object references, rather than having to compare each character of two strings.

I’m sure there are all sorts of other good reasons, too.

Cheers,

Harry O.

Just saw this over at http://www.c2.com/cgi/wiki?IwannaLearnRuby and
realised I don’t know the answer.

How are Symbol objects used?
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Symbol.html
Why should I use them to create class attributes? --GirtsKalnins

It appears to me that everywhere that you could use a symbol, you could
just as easily use a string. Is there any reason other than aesthetics
that you shouldn’t just use Strings everywhere? Are Symbols just a
hangover from an older Ruby version.

One reason: Speed.

Symbols are like Atoms in other languages – they are invariant, and a
symbol only creates a new object if it’s the first time it’s used.
They’re singletons per-value:

:foo.id == :foo.id == true
"foo".id == "foo".id == false

They’re hooked right into Ruby’s internal symbol table.

They’re also marginally easier to type.

···

On Wed, Mar 31, 2004 at 09:08:20AM +0900, Daniel Sheppard wrote:

Daniel Sheppard
http://jroller.net/page/soxbox
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################

“Daniel Sheppard” daniels@pronto.com.au schrieb im Newsbeitrag
news:EE0A2C5DE8D7CA4792891604D4FED52E016749F2@mlbmail.pronto.com.au…

Just saw this over at http://www.c2.com/cgi/wiki?IwannaLearnRuby and
realised I don’t know the answer.

How are Symbol objects used?
http://www.ruby-doc.org/docs/rdoc/1.9/classes/Symbol.html
Why should I use them to create class attributes? --GirtsKalnins

It appears to me that everywhere that you could use a symbol, you could
just as easily use a string. Is there any reason other than aesthetics
that you shouldn’t just use Strings everywhere? Are Symbols just a
hangover from an older Ruby version.

Additionally to everything mentioned by others: every string can be
converted into a legal string but not the other way round:

irb(main):007:0> “foo”.intern
=> :foo
irb(main):008:0> “\000”.intern
ArgumentError: symbol string may not contain \0' from (irb):8:in intern’
from (irb):8

Regards

robert

Symbols are like Atoms in other languages – they are invariant, and a

symbol only creates a new object if it’s the first time it’s used.

I’ve been wondering about symbols as well. What are some examples of a good
place to use symbols?

Robo

when you use something related to reflection
(method(:sym) is an example)

when you need a constant meaning something, insted of the C-like
Module::CONST
you can just use :const
:const is just like a number (immutable and so on) but it has the
plus have being meaningful.

in general, I even believe using symbols is safer than using string.
If you are using a string and do not need it’s features (take a look
at String.instance_methods ) you may think of using a symbol (even if
not everything is allowed as a symbol), avoiding possibly messing up
something by changing the string.

···

il Wed, 31 Mar 2004 23:23:46 +1200, “Robo” robo@mars.com ha scritto::

Symbols are like Atoms in other languages – they are invariant, and a

symbol only creates a new object if it’s the first time it’s used.

I’ve been wondering about symbols as well. What are some examples of a good
place to use symbols?

“gabriele renzi” surrender_it@remove.yahoo.it schrieb im Newsbeitrag
news:tncl60hesfj7dcfgl5b3ja6uio2ak95if2@4ax.com

il Wed, 31 Mar 2004 23:23:46 +1200, “Robo” robo@mars.com ha
scritto::

Symbols are like Atoms in other languages – they are invariant, and a

symbol only creates a new object if it’s the first time it’s used.

I’ve been wondering about symbols as well. What are some examples of a
good
place to use symbols?

when you use something related to reflection
(method(:sym) is an example)

when you need a constant meaning something, insted of the C-like
Module::CONST
you can just use :const
:const is just like a number (immutable and so on) but it has the
plus have being meaningful.

Yep.

in general, I even believe using symbols is safer than using string.
If you are using a string and do not need it’s features (take a look
at String.instance_methods ) you may think of using a symbol (even if
not everything is allowed as a symbol), avoiding possibly messing up
something by changing the string.

No, in that case you use #freeze. You should make it only dependent on
the semantics and not on the methods you actually use. It’s perfectly ok
to have a string constant, when it’s appropriate - even if you don’t need
to change it. Apart from that methods might not accept a Symbol. I’d
always write

File.open( “foo” ) {|io| … }

instead of

File.open( :foo ) {|io| … }

whiel yields an invalid argument anyway.

robert

I agree that it’s a question of semantics.
I just believe that in most cases a simple symbol is what is needed,
not a String.

I dont’ want to let the user do stuff like:

str=‘something’
str.freeze
…use str

when I can simply let him do
… use :something

I.e, if there is the need to have some stringish key for an hash, I’d
prefer to use a symbol over a string.

But maybe it’s just that typing “s” instead of :s is much harder on my
keyboard :slight_smile:

···

il Wed, 31 Mar 2004 14:58:20 +0200, “Robert Klemme” bob.news@gmx.net ha scritto::

in general, I even believe using symbols is safer than using string.
If you are using a string and do not need it’s features (take a look
at String.instance_methods ) you may think of using a symbol (even if
not everything is allowed as a symbol), avoiding possibly messing up
something by changing the string.

No, in that case you use #freeze. You should make it only dependent on
the semantics and not on the methods you actually use. It’s perfectly ok
to have a string constant, when it’s appropriate - even if you don’t need
to change it. Apart from that methods might not accept a Symbol.

“gabriele renzi” surrender_it@remove.yahoo.it schrieb im Newsbeitrag
news:ulgl60tb5mmeos6eoctd7po3sduifeqfki@4ax.com

in general, I even believe using symbols is safer than using string.
If you are using a string and do not need it’s features (take a look
at String.instance_methods ) you may think of using a symbol (even if
not everything is allowed as a symbol), avoiding possibly messing up
something by changing the string.

No, in that case you use #freeze. You should make it only dependent on
the semantics and not on the methods you actually use. It’s perfectly
ok
to have a string constant, when it’s appropriate - even if you don’t
need
to change it. Apart from that methods might not accept a Symbol.

I agree that it’s a question of semantics.
I just believe that in most cases a simple symbol is what is needed,
not a String.

I wouldn’t judge on the relative frequency of those situations. I just
wanted to make sure that people do not believe String and Symbol are
completely interchangable.

I dont’ want to let the user do stuff like:

str=‘something’
str.freeze
…use str

Note that you can do

str=‘something’.freeze

when I can simply let him do
… use :something

I.e, if there is the need to have some stringish key for an hash, I’d
prefer to use a symbol over a string.

True, that’s often reasonable. Although in those cases an OpenStruct or a
Struct might be more appropriate. It depends…

But maybe it’s just that typing “s” instead of :s is much harder on my
keyboard :slight_smile:

And it saves you a lot of typing (8 chars). :slight_smile:

Regards

robert
···

il Wed, 31 Mar 2004 14:58:20 +0200, “Robert Klemme” bob.news@gmx.net > ha scritto::