On Wed, Mar 31, 2004 at 09:08:20AM +0900, Daniel Sheppard wrote:
> 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.
>
> Daniel Sheppard
> http://jroller.net/page/soxbox
> #####################################################################################
> This email has been scanned by MailMarshal, an email content filter.
> #####################################################################################
>