Symbols are not variables, but a type of literal value, like numerals and quoted strings. Significantly, symbols are used to represent variables and other named values in the Ruby runtime. So when the Ruby interpreter sees the name foo
used as a variable or method name, what it looks up in the Hash of runtime values is the symbol :foo
, not the string "foo"
. This was, in fact, the original use of the term “symbol” in programming language terminology; variables, functions, constants, methods, and so on are said to be stored in the compiler or interpreter’s “symbol table”.
Pretty much any time you’re passing around the name of something in Ruby, you’re going to use a symbol. If you use method_missing
as a catch-all to implement arbitrary methods on your object class, a symbol is what it receives as an argument telling it the name of the method that was actually called. If you inspect an object with .methods
or .instance_variables
, what you get back is an array of symbols. And so on.