From: Jacob Fugal Date: 2006-01-06T01:45:43+09:00 Subject: Re: On Symbols If I may make one correction/clarification to an otherwise *excellent* explanation of the implementation... On 1/5/06, evanwebb@gmail.com wrote: > For symbols, the least significant byte is 0x0e and the upper 3 bytes > are a integer. The integer is uniquely assigned value for a string. > Think about it as the table index for a string. By using a symbol, you > basically allocate a string once and then refer to it by the index it > occupies in a special symbol table. For example, the first time the > symbol :evan is seen, a string containing "evan" is created and stored Clarification: a *C* string containing "evan" is created... > in the symbol table at, say, index 9323. The variable that was assigned > :evan gets assigned > ((9223 << 8 ) | 0x0e). The next time :evan is seen, "evan" is looked up > in the symbol table to obtain 9232 again. > > So, to review: > > a = :evan > > a.to_i # => 9232 (the index in the symbol table) > a.object_id # => 2363406 (the index << 8 | 0x0e) > a.to_s # => the reference to the string object located at > index 9232 in the symbol table Correction: a.to_s returns a reference to a new String object containing the same sequence of characters as the C string in the symbol table. This is visible when you compare the result of #object_id on subsequent calls to a.to_s: irb> a = :evan # => :evan irb> a.to_s.object_id # => 1657424 irb> a.to_s.object_id # => 1653204 > The ruby runtime rules take the integer value and apply the rules to > determine what it means. If it's odd, it's a Fixnum immediate value. If > it's 0,2,4, or 6, it's a "core" immediate value. If it has the LSB is > 0x0e, it's a symbol. Otherwise, it's a pointer to a memory address that > holds the information about the object. > > Thats the end of days leason! Hope it helps! Thanks, Evan! Aside from those minor, pedantic corrections, it was indeed an excellent lesson. Jacob Fugal