From: Austin Ziegler Date: 2005-12-30T03:19:33+09:00 Subject: Re: What is the difference between :foo and "foo" ? On 29/12/05, Steve Litt wrote: > On Thursday 29 December 2005 11:20 am, Kirk Haines wrote: >> On Thursday 29 December 2005 8:45 am, Steve Litt wrote: >>> They wouldn't exist, or they wouldn't be called symbols. If I'm >>> referring to an instance instead of a value, I could call it >>> &variable as in C, or I could call it :variable, but in that case >>> I'd call it a reference, not a symbol. >> It's not a reference. Symbols are much simpler than that. Really. >> They are just an integer with some sequence of characters to >> represent it, all wrapped up in an object. > OK, let me see if I'm correctly understanding you. > > :wilbur is an object. That object has two properties, which one could > call, in generic and not Ruby terms, a key and a value. The key is an > integer. The value is "wilbur". Neither the key nor the value can be > changed during the execution of the program. When we write: No. :wilbur is an object. It has *one* property, itself. You can ask for integer or string representations, but :wilbur's value is :wilbur and there will only ever be one :wilbur. > attr_accessor :wilbur > > what really happens is that the function attr_accessor() takes symbol > :wilbur as an argument, tacks on a @ to the string's value in order to > make the true class variable @wilbur, and then writes a tiny get and > set functions whose names are wilbur, such that: Sort of. In the link that I posted for you earlier, you'll note that I wrote an accessor generator (md5_accessor) which can be used to create accessors that deal with variables named completely differently than the accessors. The method Module#attr_accessor will take the symbol and use it as a name to create accessors (#wilbur and #wilbur=) that *coincidentally* work on a variable of the same name. It doesn't *create* that variable. > my_object.wilbur = 22 > puts my_object.wilbur > > So my initial comment that it seemed like magic is true only to the > extent that the "magic" performed by attr_accessor is to create the > set and get methods with the same name as the symbol that is its > argument. You are correct in this last. The "magic" is in attr_accessor, not in the Symbol. The Symbol is just a name. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca