struct

Symbol

Inherits Comparable / Value / Object

A symbol is a constant that is identified by a name without you having to give it a numeric value.

:hello
:welcome
:"123"
:"symbol with spaces"

See Symbol literals in the language reference.

Internally a symbol is represented as an Int32, so it's very efficient.

You can't dynamically create symbols. When you compile your program, each symbol gets assigned a unique number.

Class methods

needs_quotes?(string) : Bool

Determines if a string needs to be quoted to be used for a symbol literal.

Symbol.needs_quotes? "string"      # => false
Symbol.needs_quotes? "long string" # => true
Source

Instance methods

!=(other : Symbol) : Bool

Returns true if self is not equal to other.

Source
<=>(other : Symbol)

Compares symbol with other based on String#<=> method. Returns -1, 0 or 1 depending on whether symbol is less than, equal to, or greater than other.

See String#<=> for more information.

Source
==(other : Symbol) : Bool

Returns true if self is equal to other.

Source
clone
Source
hash(hasher)

See Object#hash(hasher)

Source
inspect(io : IO) : Nil

Returns the symbol literal representation as a string.

:crystal.inspect # => ":crystal"
Source
to_i

Returns a unique number for this symbol.

Source
to_json(json : JSON::Builder) : Nil
Source
to_json_object_key
Source
to_s(io : IO) : Nil

Appends the symbol's name to the passed IO.

:crystal.to_s # => "crystal"
Source
to_s

Returns the symbol's name as a String.

:foo.to_s           # => "foo"
:"hello world".to_s # => "hello world"
Source
to_yaml(yaml : YAML::Nodes::Builder) : Nil
Source