From: Chad Perrin Date: 2007-03-08T14:41:35+09:00 Subject: Re: Paul Graham explains Ruby symbols On Thu, Mar 08, 2007 at 02:17:13AM +0900, Rick DeNatale wrote: > On 3/6/07, Luciano Ramalho wrote: > >Paul Graham offers this excellent explanation for the symbol type: > > > >"Symbols are effectively pointers to strings stored in a hash table. > >So you can test equality by comparing a pointer, instead of comparing > >each character." [1] > > > >Of course, he's talking about symbols in Lisp, but what he says > >applies equally well to Ruby and Smalltalk. > > I find this a little too implementation-centric a description. Unfortunately, there's no way to differentiate a string literal from a symbol in a definitive manner without brushing up against implementation. At least, I haven't seen such a thing yet. > > The key aspect of symbols is that two symbols are identical if they are > equal. > The fact that there may (or may not be) a hash table used to ensure > this is irelevant. Ahh . . . but think about how they're "equal". They're equal because of the manner in which they're implemented. If you want to separate the concept of symbols from the implementation to some degree, you might explain as little of implementation as possible while still getting the point across, then say that "this could change so that symbols still behave the same way but are implemented somewhat differently, but this is how it's done right now". How exactly, other than the difference between : and '', do you differentiate a string literal from a symbol without discussing implementation? I don't much see a way to do it. Obviously, a symbol is different in easily explained ways from string variables, without having to drag implementation into it. Things aren't quite so clear-cut between symbols and string *literals*, though. > > Not long ago Matz experimented with the idea of making Symbol a > subclass of String in 1.9, but this appears to have been dropped in > more recent versions. That's interesting -- I didn't know that. Thanks for mentioning it. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] "It's just incredible that a trillion-synapse computer could actually spend Saturday afternoon watching a football game." - Marvin Minsky