From: Rick DeNatale Date: 2007-03-09T06:20:40+09:00 Subject: Re: Paul Graham explains Ruby symbols On 3/8/07, Rick DeNatale wrote: With an itchy trigger finger. > 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 is_symbol?(:a) => true is_symbol?("a") => false is_symbol?([:a, :b]) => false > > > > > > > > > > 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". Or you can turn this around and say that the equality-identity relationship is what's essential and that one way of implementing this requirement is to intern using a hash table. > > 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. The same way you can describe the properties of other objects without regard to implementation. For example we can understand how integers work without having to going into the details of how they are represented. > > 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. I'm not sure that I get your distinction between string variables and string literals. It's really a matter of the differences between string OBJECTs and symbol OBJECTs. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/