Steve said:
Long time Rubyists, people with heavy CS studies, and people who have
experience in languages with symbols (LISP, I guess) tend to talk more about
how it's implemented, while programmers (probably without CS degrees)
migrating from languages not containing symbols are more interested in what
it does than how it's implemented.
I asked if it's an object with 2 attributes, an integer key and a string
value. Someone says no, it's an object with one attribute that can be seen 2
ways, a string or an integer. From my point of view, he's saying the same
thing. I was describing what it can do for me, he was describing something
truer to the implementation, but when actually using the little bugger either
explanation will lead to correct programming, at least if I understand it
correctly (and I think I now understand it correctly, at least correctly
enough to know how and when to use a symbol).
I'm somewhat new to ruby, and for me it has made most sense to do an ri
on Symbol to check out the api. When I first ran across symbols in the
Pickaxe it didn't make much sense. I started out just taking it on
blind faith how they get used in different frameworks api's. Seems
like what symbols are is different from how they might be best used.
Here's something I wrote up to play around with how they work. I was
surprized to find out that any identifier (class, method, variable,
etc) in a ruby script gets turned into a symbol.
Bill
# Definition: A symbol is an object of class Symbol. The symbol
# has only one instance with given name that can be found by
# calling the method id2name or to_s. The symbol has a unique
# integer value that can be found by calling the to_i method.
# The Symbol class method all_symbols will give an array of all
# symbols. A symbol is automatically generated for any
# identifier used or seen within a ruby program including
# constants, classes, methods, variables, etc. Once a symbol is
# created it is never garbage collected.
def find_and_display_symbol(symbol_name)
found_symbol = Symbol.all_symbols.find { |s| s.to_s == symbol_name }
if (found_symbol != nil)
puts "symbol found with name: #{found_symbol.to_s} and " +
"id: #{found_symbol.to_i}\n\n"
else
puts "unable to find symbol #{symbol_name}\n\n"
end
end
# create a symbol object called symbol_name
:my_new_symbol
# display the newly created symbol by going through all symbols
# see find_and_display_symbol function below
find_and_display_symbol('my_new_symbol')
# symbols apparently don't have to be a legal identifier
:"Hello World!"
find_and_display_symbol("Hello World!")
#local variable creates a symbol
silly_local_variable = "Hello"
find_and_display_symbol('silly_local_variable')
# Any referenced identifier will create a symbol, even if the symbol is
# undefined:
defined? undefined_variable
find_and_display_symbol('undefined_variable')
# Symbols already exist for builtin classes and methods
find_and_display_symbol('String')
find_and_display_symbol('each')
find_and_display_symbol('$LOAD_PATH')
# list all symbols
sym_array = Symbol.all_symbols
sym_array.sort! {|a,b| a.to_s <=> b.to_s }
sym_array.each {|s| puts "name: #{s.to_s} id: #{s.to_i}\n" }