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.
> 
> Maybe that's the reason for using symbols.

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.