From: Rick DeNatale Date: 2007-05-19T08:28:24+09:00 Subject: Re: Why was the "Symbol is a String"-idea dropped? On 5/18/07, Robert Dober wrote: > On 5/18/07, Rick DeNatale wrote: > > On 5/18/07, Paul Brannan wrote: > > > On Fri, May 18, 2007 at 03:17:01AM +0900, dblack@wobblini.net wrote: > > > > Yes, it's the immutable/immediate thing that symbols have in common > > > > with fixnums and that neither has in common with strings. > > > > > > Frozen strings are immutable. > > > > But not immediate. > > > > What about > > %f{This is sooo cooooold} << "!" > > TypeError: can't modify frozen string > Just an idea. That's the immutable part, but a = "abc".freeze b = "abc".freeze c = :abc d = :abc a.object_id => -606341628 b.object_id => -606347008 c.object_id => 343218 d.object_id => 343218 The key difference is that there's only one instance of a symbol with a given string representation. The shorthand way of saying this is that symbols, like fixnums are immediate. Which is a sufficent but not necessary condition, it crosses the line a bit in describing both the identity relationship requirement AND the implementation. Most normal objects are referenced at the C level by an internal value which is a pointer to the objects state representation in memory. Since objects are aligned at least on a word boudary, all normal object pointers will have the 2 least significant bits as zero. They will also be non-zero A few objects are immediate which means that they are referenced at the C level by a representation whose value is not a pointer. Fixnums are represented by shifting the C representation left one bit and turning on the low-order bit. False is represented by 0, True by 2, and Nil by 4. Ruby symbols are represented by a value computed by shifting the symbols integer representation left 8 bits and setting the low-order byte to 0xFF representation As I said, it's not essential that symbols be immediate, for example interning a string could create a Symbol instance which was frozen and registered in a global symbol table, i.e. the multiton pattern, but the current implementation no doubt has some advantages in either low-level mechanism performance, supporting some niche in ruby legacy, or both. -- 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/