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.
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
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 …
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.
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.
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:
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.
#####################################################################################
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
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?
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
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
…
···
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.
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
And it saves you a lot of typing (8 chars).
Regards
robert
···
il Wed, 31 Mar 2004 14:58:20 +0200, “Robert Klemme” bob.news@gmx.net > ha scritto::