From: Rick DeNatale Date: 2007-11-02T06:37:47+09:00 Subject: Re: Ahh, Ruby's symbol table (was Re: Garbage collection and symbols) On 11/1/07, Randy Kramer wrote: > * one difference between a C pointer and a Ruby reference that I'm "walking > away with" is that the C pointer is stored in a location (and takes up space) > where the programmer can access it (and can notice that it takes up space). > In contrast, a Ruby variable (which is a reference) is not as easily > accessible to the programmer, but it does take up space in the (Ruby's) >> symbol hash table. Not quite. The big differences. Ruby variables always reference objects. Although the implementation is 'pointer-like' References aren't always implemented as pointers. For example Fixnums (integers representable by 1-bit less than a machine word) are represented by a manipulation of the integer value (the MRI implementation represents an integer i as a reference (i*2)+1. Ruby variables, unlike C pointer variables can't themselves be referenced indirectly. There's no direct correspondence between variables and symbols. Instance variables and class variables do use a symbol lookup in a hash to find them by name, but local variables will iive on the stack, and slots in Arrays' Hashes and other primitive ruby objects are nameless variables which, since they are variables hold references. In MRI symbols are, or have been, used to represent instance/class variable namss (as I mentioned), and also method names Methods are found by looking up the name in a series of hash lookups, at least notionally. So besides creating symbols with a :symbol literal they may also get defined when you define an instance variable, class variable, or method with a previously unseen symbol. Again this is somewhat implementation dependent. > * in Ruby's symbol hash table, a symbol is stored (in the hash table) with > all the characters that make up the symbol. For example, if I have a > symbol :test, in Ruby's hash table, the string "test" is stored. (I'm > guessing as the key--I presume the value is something that somehow eventually > is translated into a location in memory.) Again, this is implementation dependent. It's not specified by the language definition, even if there were one. > Is there a way for the programmer to manipulate Ruby's symbol hash table > within Ruby more directly than occurs when defining variables or symbols? In the way I understand you to mean, only by writing an extension in C. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/