When I was first learning Ruby, symbols were a bit of a mystery.
I understand them now, and in hindsight, I don’t really understand
why they were so confusing. In teaching other people about Ruby and
in reading ruby-talk it is quite clear that symbols are an early
stumbling block for learning Ruby. I’d take an educated guess that
people learning Rails (or any other Ruby framework) struggle with
trying to understand if symbols are a magic feature of the framework
or the language or something else.
My question to the list is, why? What is it about Symbols that
creates such confusion? I’m getting ready to prepare a two-hour
“Intro to Ruby” seminar and I’d like to find an approach to Symbols
that cuts through the confusion.
One thought that I’ve had is that Ruby’s reflection capabilities
expose the names of various internal structures (classes, methods,
modules, constants) in a way that is quite foreign to many programmers.
For example, in C you don’t have any runtime access to the symbol table
so you would never see a value that could be mapped to a function like
‘printf’, ‘scanf’, or ‘main’ unless the programmer took the trouble to
construct an explicit symbol table (mapping strings to function
for example). An explicit table isn’t much of a mystery.
When you switch to a language like Ruby, Smalltalk, and certainly the
entire family of Lisp languages you are now in an environment where the
data structures of the language runtime are exposed to the programmer.
For a while, the novice will struggle because their mental model of
a runtime environment just doesn’t match their new reality and symbols
just happen to be the bridge between the two worlds. One of the first
features of Ruby that many people learn are the ‘attr*’ methods. Right
off the bat, the novice is exposed to the mappings from strings to
symbols to methods.
Do folks coming from the Lisp/Scheme world have trouble with symbols or
do they absorb symbols right away because they already struggled with
the concept when learning about atoms?