In addition to what Markus has written, you can also read the (somewhat
brief) explanation at: The Ruby Language.
IIUC, symbols are implemented internally as immutable strings. Take a
look at String#intern or String#to_sym ( class String - RDoc Documentation).
I agree Markus's explanation was excellent. (I didn't even know that Lisp
had symbols.)
I'd only add that one common use for a Ruby Symbol is to stand for a method
name. (That's a slightly misleading statement perhaps -- attr_accessor takes
one symbol and creates two methods, but their names are both based on the
symbol.)
For example, attr_accessor takes symbols, as does Object#send.
I'd only add that one common use for a Ruby Symbol is to stand for a method
name. (That's a slightly misleading statement perhaps -- attr_accessor takes
one symbol and creates two methods, but their names are both based on the
symbol.)
For example, attr_accessor takes symbols, as does Object#send.
Why? Speed? I imagine that, in both cases, a string could have been used as well. A string, though, would let you craft an invalid method name.
I'd only add that one common use for a Ruby Symbol is to stand for a method
name. (That's a slightly misleading statement perhaps -- attr_accessor takes
one symbol and creates two methods, but their names are both based on the
symbol.)
For example, attr_accessor takes symbols, as does Object#send.
Why? Speed? I imagine that, in both cases, a string could have been used as well. A string, though, would let you craft an invalid method name.
Maybe that's the reason for using symbols.
Well, many times a string will work in place of a symbol. It depends on
the method being called.
Two other observations:
1. It's possible to call otherwise illegal method names via send
2. It's possible to include non-alphanumeric chars in a symbol
No, symbols are used because symbols are globally unique. You can have
two or more strings that comprise the same sequence of characters yet
are different objects, but two occurrences of the same symbol will refer
to the same object, no matter how you get to them. Further, strings can
be modified whereas symbols can not.
When you use strings to mess with methods it internally converts the
strings to symbols (with intern) so that methods behave the way you
expect.
-- Markus
P.S. Hashes also have some magic going on to do "what you expect" but
they _don't_ convert strings to symbols so the semantics are slightly
different. That's why we need a Hash#reindex method but not a
"Kernel#reindex_methods" method.
ยทยทยท
On Sun, 2004-09-19 at 21:17, James Britt wrote:
Hal Fulton wrote:
...
>
> I'd only add that one common use for a Ruby Symbol is to stand for a method
> name. (That's a slightly misleading statement perhaps -- attr_accessor
> takes
> one symbol and creates two methods, but their names are both based on the
> symbol.)
>
> For example, attr_accessor takes symbols, as does Object#send.
Why? Speed? I imagine that, in both cases, a string could have been
used as well. A string, though, would let you craft an invalid method
name.