W3cubDocs

/Crystal 0.31

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
:"123"
:"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.

Included Modules

Defined in:

primitives.cr
symbol.cr
json/pull_parser.cr
json/to_json.cr
yaml/to_yaml.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.needs_quotes?(string) : BoolSource

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) : BoolSource

Returns true if self is not equal to other.

def <=>(other : Symbol)Source

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.

def ==(other : Symbol) : BoolSource

Returns true if self is equal to other.

def ==(other : JSON::PullParser::Kind)Source

DEPRECATED JSON::PullParser#kind now is an enum, please use that instead of symbols

def cloneSource

def hash(hasher)Source

def inspect(io : IO) : NilSource

Returns the symbol literal representation as a string.

:crystal.inspect # => ":crystal"

def to_i : Int32Source

Returns a unique number for this symbol.

def to_json(json : JSON::Builder)Source

def to_json_object_keySource

def to_s(io : IO) : NilSource

Appends the symbol's name to the passed IO.

:crystal.to_s # => "crystal"

def to_s : StringSource

Returns the symbol's name as a String.

:foo.to_s           # => "foo"
:"hello world".to_s # => "hello world"

def to_yaml(yaml : YAML::Nodes::Builder)Source

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/Symbol.html