From: Rick DeNatale Date: 2007-03-09T06:11:43+09:00 Subject: Re: Paul Graham explains Ruby symbols On 3/8/07, Chad Perrin wrote: > 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. lets see def is_symbol?(arg) arg.object_id == arg.to_sym.object_id rescue false end > > > > > > 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 > > -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ IPMS/USA Region 12 Coordinator http://ipmsr12.denhaven2.com/ Visit the Project Mercury Wiki Site http://www.mercuryspacecraft.com/