I was actually doing really well until the strange discussion involving
throwing constants into the mix showed up, so I’m ignoring that.
Steve’s Newby Guide was excellent, if overly-complicated.
Extremely useful item (I think from Gregory, but I’m not sure):
attr_accessor "liquids", "solids"
as effective and functional as
attr_accessor :liquids, :solids
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.
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.
Anyway, I’m quite confident I know what a Symbol is now. It’s an
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”
makes not one iota of sense unless one already understands all the
complex ramifications of those quote marks. And given the extra magic
powers of double quotes vs. single quotes, well, ick.
A couple of people have tried to advance the idea that a Symbol is a
name. I think that’s a terrible mistake. On the one hand, here in the
real world, people will say something like “Dave is a boy’s name,” but
what that sentence really means is “The word ‘dave’ is normally used
only as a name for males.”
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
what I’m actually saying is “Dear routine/module/function named
‘attr_accessor,’ please create a couple of new methods for my
Class/Module/whatever. Use that string I sent you as the name of one
which will return the value of a variable also named that, and use it
yet again to build a setter method.” That’ll get me a method named
“dave”, a method named “dave=”, and, hmm. I don’t know if “dave=” will
work if I don’t explicitly set @dave to a value at some point, although
I think it will. I’d have to check that to find out.
Anyway, that’s not the important point. Nor is the potential
“efficiency” of Symbols vs. Strings. Good grief, have you people looked
at the millisecond differences that I’ve been seeing thrown around? We
beginners couldn’t care less; we just want it to not make lots of
errors when it runs, or doesn’t run.
What I’ve learned:
A Symbol is just a modest, simple string. It’s optional; I could
always use a String instead. It’s good for naming things that are
‘inside’ my program, and that won’t need to be modified, input, output,
or generally fiddled with.
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.