On Wed, 28 Dec 2005 19:47:16 -0000, Steve Litt <slitt / earthlink.net> wrote: >> http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.re >> d > > The preceding URL tells me unequivically that symbols aren't strings, but > really doesn't tell me too much about what they are, other than what, > names??? > I agree with Jim (obviously ;)) that describing symbols as immutable strings isn't ideal, but it did help me break away from the idea that they worked on some kind of internal voodoo. An object with a name seems a good way to put it - maybe 'an object that is a name, by which it can be referenced anywhere'. So :foo is just the name, 'foo', as an object. A singleton object. Kind of like the literal '4' - wherever you use that literal, you'll get the same instance of Fixnum (incidentally, with object_id 9), whether you mean four loops, or four bananas, since four is four the same one will do. > I still don't understand why it's > > attr_reader :fname, :lname > > instead of > > attr_reader @fname, @lname > > How does attr_reader know that :fname corresponds to @fname. Seems like > magic > to me. > You're looking at it backwards. You give attr_reader a name (or several). It then takes those names, and just creates methods with each one. There's no connection between the symbols, and the methods - it's just like passing in a string (which you can actually do instead) except that, instead of creating a new string with the characters 'lname' or whatever, it just grabs the single symbol with that name, or makes it if it doesn't already exist. It saves memory, and is better on performance in many types of system. The reader method accesses an instance variable, again named from the symbol, and it gets created automatically at startup. So the symbol just gives you the name - it's up to you to supply the context (like 'an attribute reader with this name' above). -- Ross Bamford - rosco / roscopeco.remove.co.uk