I’m just not getting the Symbol type.
Symbols are very much like strings, but with some important differences.
When you construct two strings with the same contents, e.g.
s1 = "Hello"
s2 = "Hello"
it is true that the strings are equivalent, i.e. both of these
expressions:
s1 == s2
s1.eql? s2
are true. But s1 and s2 don’t refer to the same string object, i.e.
s1.equal? s2
is not true. In other words, you’ve got two little blobs of memory
storing the same data. Sometimes, that’s what you intended, but in a lot
of cases – especially where you’re using strings as constants – it’s a
waste.
That’s where symbols come in. When you construct a symbol object it
creates a unique entry in an (internal) symbol table for this string
that all future symbol reference will refer to. In other words,
s1 = :Hello
s2 = :Hello
s1 == s2 # this is still true
s1.eql? s2 # so is this
s1.equal? s2 # this is now also true
What does this buy me? When would I want to use it?
If nothing else, it may help to reduce the amount of string allocation,
deallocation, etc. in your programs. For example, if I wrote the
following snippet in Ruby:
allUsers.each do |userName|
if userName == "Christopher"
# found a match
end
end
Ruby would (presumably) allocate a new (temporary) string object, with
the contents “Christopher”, for each pass through the loop. It would
also perform that many character-by-character string comparisons. For a
lot of user names, that’s going to be slow. In contrast, this bit of code:
allUsers.each do |userName|
if userName.intern == :Christopher
# found a match
end
end
would run significantly faster since the “Christopher” symbol gets added
to the symbol table once and subsequent references just point to that
entry. Also, since Symbol objects mainly consist of an index into the
symbol table, comparing symbols is very fast – as fast as comparing two
integer values to each other. This code could be made even faster if the
“allUsers” array stored symbols directly instead of storing strings, but
you get the idea.
What is it analogous to in another language?
I think symbols probably got popular with LISP (where they’re called
“atoms”, and are one of the basic data types). I remember that Python
supported this idea (often called “interning” strings) via an intern()
function. C and C++ don’t have any (standard) equivalent.
Hope this helps,
Lyle
···
christopher.j.meisenzahl@citicorp.com wrote: