struct Symbol

Overview

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

:hello
:welcome
:"symbol with spaces"

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.

Superclass hierarchy

Object
Value
Symbol

Included Modules

Comparable(Symbol)

Defined in:

Class Method Summary

Instance Method Summary

Class Method Detail

def self.needs_quotes?(string)

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

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

Instance Method Detail

def <=>(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_symbol. See String#<=> for more information.


def inspect(io : IO)

Returns the symbol literal representation as a string.

:crystal.inspect # => ":crystal"

def to_s(io : IO)

Appends the symbol's name to the passed IO.

:crystal.to_s # => "crystal"