From: Chad Perrin Date: 2006-01-05T17:13:15+09:00 Subject: Re: Demystifying Symbols. On Thu, Jan 05, 2006 at 04:25:31PM +0900, Dave Howell wrote: > > So Steve, or anybody, I've learned that I can :mysymbol.to_i and I get > an integer back. OK, I give up. What possible use do I have for this > zany parlor trick? I'll hazard a guess that, as a "normal" programmer, > not doing system-level stuff, not extending Ruby, not trying to do > something dense and clever and incomprehensible...I don't. The integer value can be regarded as a side effect of the way symbols are managed. They're stored as values in a hash table with the integers as keys, basically. It's the atomic number-like value of the symbol, and the "it has always been there" behavior[1] of it when first defined, that the language feature is after -- not the integers associated with symbols in the hash table. If you find a good use for the .to_i behavior, though, have at it. At least, that's my understanding. > > In fact, somebody correct me if I'm wrong, but I could actually never > use a Symbol in a single line of Ruby code and I'd still be able to get > everything done that I might reasonably want to. Symbols are useful for metaprogramming techniques, and can be used as well for some minor performance increases. For most of the programming people do, however, they're far from necessary. Note: My comment about symbols being useful for metaprogramming techniques is based on my understanding of symbols in a Lisp context, not a Ruby context. I'm still trying to get a handle on how analogous the two are to each other -- a lot, clearly, but the devil's in the details (so to speak). I'm nothing like a Ruby expert, and there's a lot of context to learn before I can be sure about those details. > > Stop!! Put your geek away! I didn't say it was a String. It's a string. > It's a Merriam-Webster "string1" 5b(2) 'series of like objects (e.g. > characters, bits, words)'. It's just a series of characters cemented > together, bless its little stable self. What Ruby calls a String is > actually some kind of pandimensional quasi-magic method-possessing > self-modifying Object, and not an ordinary string at all. > > Note that the neologism > :symbol is to "symbol" as 1 is to "1" For purposes of my analogy to that effect a few days ago, that could as easily have been single quotes as double quotes, in case there's any confusion on the matter. My point was the relative characteristics of a Ruby string literal as compared with numbers and symbols. > > For me, :dave doesn't become a name until I decide what it's the name > *of*. :dave is just a boring old string, and when I say > attr_accessor :dave This makes me want to draw analogies between Ruby symbols and spoken language phonemes, which while interesting and in some respects accurate would probably not be helpful to many people. > > A Symbol is a non-variable variable (aka a constant) that always and > only contains its own name. > > > Seriously, as a newbie, that last sentence is all I need to know. And, > quite honestly, probably all I'll *ever* need to know about Symbols. That depends on just how advanced your programming gets. Programs that write themselves are nothing to sneeze at. One of these days, I'll be at a point where I can actually take advantage of that sort of thing -- but I'm not there yet. -- Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ] This sig for rent: a Signify v1.14 production from http://www.debian.org/