Symbols are named identifiers that can be used to refer to a value.
Symbols are created through a symbol literal, which is by prefixing a name with a :
character, e.g. :foo
.
They also allow for being written with quotes, e.g. :"foo"
, which allows, for example, spaces in the name.
:foo # => :foo
:"foo boo" # => :"foo boo"
Symbols are used in many places in the language, including as keys in namedtuples and to represent method and variable names.
Symbols in Crystal are quite different from Ruby. In Crystal a symbol is a type of constant and is thereby is assigned at compile time. This means that symbols can't be created dynamically, which is possible in Ruby.
Symbols in Crystal are represented as Int32
s which makes them very efficient.
What makes symbols different from strings is that they are identifiers and do not represent data or text. This means that two symbols with the same name are always the same object.
"foo".object_id # => 60
"foo".object_id # => 80
:foo.object_id # => 1086748
:foo.object_id # => 1086748
Symbols can be converted to strings but not vice versa. This is because symbols are created at compile time and strings are created at runtime.
:foo.to_s # => "foo"