- README

- Top Level Namespace
- Adler32
- ArgumentError
- Array
- Atomic
- Base64
  - Error
- Benchmark
  - BM
    - Job
    - Tms
  - IPS
    - Entry
    - Job
- BigFloat
- BigInt
- BigRational
- BitArray
- Bool
- Box
- Bytes
- Channel
  - Buffered
  - ClosedError
  - ReceiveAction
  - SelectAction
  - SendAction
  - Unbuffered
- Char
  - Reader
- Class
- Colorize
  - Object
  - ObjectExtensions
- Comparable
- Complex
- Concurrent
  - CanceledError
- CRC32
- Crypto
  - Bcrypt
    - Error
    - Password
  - Blowfish
  - Subtle
- Crystal
  - Macros
    - And
    - Arg
    - ArrayLiteral
    - Assign
    - ASTNode
    - BinaryOp
    - Block
    - BoolLiteral
    - Call
    - Case
    - Cast
    - CharLiteral
    - ClassDef
    - ClassVar
    - Def
    - Expressions
    - Generic
    - Global
    - HashLiteral
    - If
    - ImplicitObj
    - InstanceSizeOf
    - InstanceVar
    - IsA
    - Macro
    - MacroId
    - MetaVar
    - MultiAssign
    - NamedArgument
    - NamedTupleLiteral
    - NilableCast
    - NilLiteral
    - Nop
    - Not
    - NumberLiteral
    - Or
    - Out
    - Path
    - PointerOf
    - RangeLiteral
    - ReadInstanceVar
    - RegexLiteral
    - Require
    - RespondsTo
    - SizeOf
    - Splat
    - StringInterpolation
    - StringLiteral
    - SymbolLiteral
    - TupleLiteral
    - TypeDeclaration
    - TypeNode
    - UnaryExpression
    - UninitializedVar
    - Union
    - Var
    - VisibilityModifier
    - When
    - While
  - System
- CSV
  - Builder
    - Row
  - Error
  - Lexer
  - MalformedCSVError
  - Parser
  - Row
  - Token
    - Kind
- Debug
  - DWARF
    - Abbrev
      - Attribute
    - AT
    - FORM
    - Info
      - Value
    - LineNumbers
      - Register
      - Row
      - Sequence
    - LNE
    - LNS
    - Strings
    - TAG
  - ELF
    - Endianness
    - Error
    - Ident
    - Klass
    - Machine
    - OSABI
    - SectionHeader
      - Flags
      - Type
    - Type
- Deque
- Digest
  - Base
  - MD5
  - SHA1
- Dir
- DivisionByZero
- DL
- ECR
- Enum
- Enumerable
  - EmptyError
- ENV
- Errno
- Exception
- Fiber
- File
  - Stat
- FileUtils
- Flate
  - Error
  - Reader
  - Strategy
  - Writer
- Float
  - Primitive
  - Printer
    - CachedPowers
      - Power
    - DiyFP
    - Grisu3
    - IEEE
- Float32
- Float64
- GC
  - Stats
- Gzip
  - Error
  - Header
  - Reader
  - Writer
- Hash
- HTML
- HTTP
  - Client
    - BodyType
    - Response
  - CompressHandler
  - ComputedContentTypeHeader
  - Cookie
  - Cookies
  - ErrorHandler
  - FormData
    - Builder
    - Error
    - FileMetadata
    - Parser
    - Part
  - Handler
    - Proc
  - Headers
  - LogHandler
  - Multipart
    - Builder
    - Error
    - Parser
  - Params
    - Builder
  - Request
  - Server
    - Context
    - RequestProcessor
    - Response
  - StaticFileHandler
    - DirectoryListing
  - WebSocket
  - WebSocketHandler
- Indexable
- IndexError
- INI
- Int
  - Primitive
  - Signed
  - Unsigned
- Int16
- Int32
- Int64
- Int8
- InvalidByteSequenceError
- IO
  - Buffered
  - ByteFormat
    - BigEndian
    - LittleEndian
    - NetworkEndian
    - SystemEndian
  - Delimited
  - EncodingOptions
  - EOFError
  - Error
  - FileDescriptor
  - Hexdump
  - Memory
  - MultiWriter
  - Seek
  - Sized
  - Timeout
- IPSocket
- Iterable
- Iterator
  - IteratorWrapper
  - Stop
- JSON
  - Any
  - Builder
    - ArrayState
    - DocumentEndState
    - DocumentStartState
    - ObjectState
    - StartState
    - State
  - Error
  - Lexer
  - ParseException
  - Parser
  - PullParser
  - Token
  - Type
- KeyError
- Levenshtein
  - Finder
- LLVM
  - ABI
    - AArch64
    - ArgKind
    - ArgType
    - ARM
    - FunctionType
    - X86
    - X86_64
      - RegClass
  - AtomicOrdering
  - AtomicRMWBinOp
  - Attribute
  - AttributeIndex
  - BasicBlock
  - BasicBlockCollection
  - Builder
  - CallConvention
  - CodeGenFileType
  - CodeGenOptLevel
  - CodeModel
  - Context
  - DIBuilder
  - DIFlags
  - DwarfTag
  - DwarfTypeEncoding
  - Function
  - FunctionCollection
  - FunctionPassManager
    - Runner
  - GenericValue
  - GlobalCollection
  - InstructionCollection
  - IntPredicate
  - JITCompiler
  - Linkage
  - MemoryBuffer
  - Module
  - ModuleFlag
  - ModulePassManager
  - OperandBundleDef
  - ParameterCollection
  - PassManagerBuilder
  - PassRegistry
  - PhiTable
  - RealPredicate
  - RelocMode
  - Target
  - TargetData
  - TargetMachine
  - Type
    - Kind
  - Value
    - Kind
  - ValueMethods
  - VerifierFailureAction
- Logger
  - Formatter
  - Severity
- Markdown
  - HTMLRenderer
  - Parser
    - PrefixHeader
    - UnorderedList
  - Renderer
- Math
- Mutex
- NamedTuple
- Nil
- Number
  - Primitive
- OAuth
  - AccessToken
  - Consumer
  - Error
  - RequestToken
- OAuth2
  - AccessToken
    - Bearer
    - Mac
  - Client
  - Error
  - Session
- Object
- OpenSSL
  - Digest
    - Error
    - UnsupportedError
  - DigestBase
  - DigestIO
    - DigestMode
  - Error
  - HMAC
  - MD5
  - SHA1
  - SSL
    - Context
      - Client
      - Server
    - Error
    - ErrorType
    - Modes
    - Options
    - Socket
      - Client
      - Server
    - VerifyMode
    - X509VerifyFlags
- OptionParser
  - Exception
  - InvalidOption
  - MissingOption
- PartialComparable
- Pointer
  - Appender
- PrettyPrint
- Proc
- Process
  - Env
  - Status
  - Stdio
  - Tms
- Random
  - ISAAC
  - MT19937
  - PCG32
  - System
- Range
- Readline
  - CompletionProc
  - KeyBindingProc
- Reference
- Reflect
- Regex
  - MatchData
  - Options
- SecureRandom
- Set
- Signal
- Slice
- Socket
  - Address
  - Addrinfo
  - Error
  - Family
  - IPAddress
  - Protocol
  - Server
  - Type
  - UNIXAddress
- Spec
  - Expectations
  - Methods
  - ObjectExtensions
- StaticArray
- String
  - Builder
  - RawConverter
- StringPool
- StringScanner
- Struct
- Symbol
- System
- TCPServer
- TCPSocket
- Tempfile
- Termios
  - AttributeSelection
  - BaudRate
  - ControlMode
  - InputMode
  - LineControl
  - LocalMode
  - OutputMode
- Time
  - DayOfWeek
  - EpochConverter
  - EpochMillisConverter
  - Format
    - Error
  - Kind
  - MonthSpan
  - Span
- Tuple
- TypeCastError
- UDPSocket
- UInt16
- UInt32
- UInt64
- UInt8
- Unicode
  - CaseOptions
- Union
- UNIXServer
- UNIXSocket
- URI
  - Error
- Value
- WeakRef
- XML
  - Attributes
  - AttributeType
  - Builder
  - Error
  - HTMLParserOptions
  - Namespace
  - Node
  - NodeSet
  - ParserOptions
  - Reader
  - SaveOptions
  - Type
  - XPathContext
- YAML
  - Any
  - Builder
  - Error
  - EventKind
  - ParseException
  - Parser
  - PullParser
  - Type
- Zip
  - CompressionMethod
  - Error
  - File
    - Entry
  - FileInfo
  - Reader
    - Entry
  - Writer
    - Entry
- Zlib
  - Error
  - Reader
  - Writer

# abstract struct Enum

- Enum
- Value
- Object

## Overview

Enum is the base type of all enums.

An enum is a set of integer values, where each value has an associated name. For example:

    enum Color
      Red   # 0
      Green # 1
      Blue  # 2
    end

Values start with the value `0` and are incremented by one, but can be overwritten.

To get the underlying value you invoke value on it:

    Color::Green.value # => 1

Each constant (member) in the enum has the type of the enum:

    typeof(Color::Red) # => Color

### Flags enum

An enum can be marked with the `@[Flags]` attribute. This changes the default values:

    @[Flags]
    enum IOMode
      Read  # 1
      Write # 2
      Async # 4
    end

Additionally, some methods change their behaviour.

### Enums from integers

An enum can be created from an integer:

    Color.new(1).to_s # => "Green"

Values that don't correspond to an enum's constants are allowed: the value will still be of type Color, but when printed you will get the underlying value:

    Color.new(10).to_s # => "10"

This method is mainly intended to convert integers from C to enums in Crystal.

### Question methods

An enum automatically defines question methods for each member, using `String#underscore` for the method name.

- In the case of regular enums, this compares by equality (`#==`).
- In the case of flags enums, this invokes `#includes?`.

For example:

    color = Color::Blue
    color.red?  # => false
    color.blue? # => true

    mode = IOMode::Read | IOMode::Async
    mode.read?  # => true
    mode.write? # => false
    mode.async? # => true

This is very convenient in `case` expressions:

    case color
    when .red?
      puts "Got red"
    when .blue?
      puts "Got blue"
    end

## Included Modules

- Comparable(self)

## Defined in:

## Constructors

- .from_value(value) : self

  Returns the enum member that has the given value, or raises if no such member exists.

- .from_value?(value) : self?

  Returns the enum member that has the given value, or `nil` if no such member exists.

- .new(pull : JSON::PullParser)
- .new(pull : YAML::PullParser)
- .parse(string) : self

  Returns the enum member that has the given name, or raises `ArgumentError` if no such member exists.

- .parse?(string) : self?

  Returns the enum member that has the given name, or `nil` if no such member exists.

## Class Method Summary

- .each(&block)

  Iterates each member of the enum.

- .names : Array(String)

  Returns all enum members as an `Array(String)`.

- .values : Array(self)

  Returns all enum members as an `Array(self)`.

## Instance Method Summary

- #&(other : self)

  Returns the enum member that results from applying a logical "and" operation between this enum member's value and other.

- #+(other : Int)

  Returns the enum member that results from adding other to this enum member's value.

- #-(other : Int)

  Returns the enum member that results from subtracting other to this enum member's value.

- #<=>(other : self)

  Compares this enum member against another, according to their underlying value.

- #==(other : self)

  Returns `true` if this enum member and other have the same underlying value.

- #^(other : self)

  Returns the enum member that results from applying a logical "xor" operation between this enum member's value and other.

- #clone
- #each(&block)

  Iterates each values in a Flags Enum.

- #hash

  Returns a hash value.

- #includes?(other : self)

  Returns `true` if this enum member's value includes other.

- #to_f32 : Float32

  Returns the value of this enum member as a `Float32`

- #to_f64 : Float64

  Returns the value of this enum member as a `Float64`

- #to_i : Int32

  Returns the value of this enum member as an `Int32`.

- #to_i16 : Int16

  Returns the value of this enum member as a `Int16`

- #to_i32 : Int32

  Returns the value of this enum member as a `Int32`

- #to_i64 : Int64

  Returns the value of this enum member as a `Int64`

- #to_i8 : Int8

  Returns the value of this enum member as a `Int8`

- #to_json(json : JSON::Builder)
- #to_s(io : IO) : Nil

  Appends a `String` representation of this enum member to the given io.

- #to_s : String

  Returns a `String` representation of this enum member.

- #to_u16 : UInt16

  Returns the value of this enum member as a `UInt16`

- #to_u32 : UInt32

  Returns the value of this enum member as a `UInt32`

- #to_u64 : UInt64

  Returns the value of this enum member as a `UInt64`

- #to_u8 : UInt8

  Returns the value of this enum member as a `UInt8`

- #to_yaml(yaml : YAML::Builder)
- #|(other : self)

  Returns the enum member that results from applying a logical "or" operation between this enum member's value and other.

- #~

  Returns the enum member that results from applying a logical "not" operation of this enum member's value.

## Macro Summary

- flags(*values)

  Convenience macro to create a combined enum (combines given members using `#|` (or) logical operator)

### Instance methods inherited from module `Comparable(self)`

<(other : T) <, <=(other : T) <=, <=>(other : T) <=>, ==(other : T) ==, >(other : T) >, >=(other : T) >=

### Instance methods inherited from struct `Value`

==(other) ==, dup dup

### Instance methods inherited from class `Object`

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

### Constructor methods inherited from class `Object`

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml

## Constructor Detail

def self.from_value(value) : self #

Returns the enum member that has the given value, or raises if no such member exists.

    Color.from_value(0) # => Color::Red
    Color.from_value(1) # => Color::Green
    Color.from_value(2) # => Color::Blue
    Color.from_value(3) # raises Exception

def self.from_value?(value) : self? #

Returns the enum member that has the given value, or `nil` if no such member exists.

    Color.from_value?(0) # => Color::Red
    Color.from_value?(1) # => Color::Green
    Color.from_value?(2) # => Color::Blue
    Color.from_value?(3) # => nil

def self.new(pull : JSON::PullParser) #
def self.new(pull : YAML::PullParser) #
def self.parse(string) : self #

Returns the enum member that has the given name, or raises `ArgumentError` if no such member exists. The comparison is made by using `String#camelcase` and `String#downcase` between string and the enum members names, so a member named "FourtyTwo" or "FOURTY_TWO" is found with any of these strings: "fourty_two", "FourtyTwo", "FOURTY_TWO", "FOURTYTWO", "fourtytwo".

    Color.parse("Red")    # => Color::Red
    Color.parse("BLUE")   # => Color::Blue
    Color.parse("Yellow") # raises ArgumentError

def self.parse?(string) : self? #

Returns the enum member that has the given name, or `nil` if no such member exists. The comparison is made by using `String#camelcase` and `String#downcase` between string and the enum members names, so a member named "FourtyTwo" or "FOURTY_TWO" is found with any of these strings: "fourty_two", "FourtyTwo", "FOURTY_TWO", "FOURTYTWO", "fourtytwo".

    Color.parse?("Red")    # => Color::Red
    Color.parse?("BLUE")   # => Color::Blue
    Color.parse?("Yellow") # => nil

## Class Method Detail

def self.each(&block) #

Iterates each member of the enum. It won't iterate the `None` and `All` members of flags enums.

    IOMode.each do |member, value|
      # yield IOMode::Read, 1
      # yield IOMode::Write, 2
      # yield IOMode::Async, 3
    end

def self.names : Array(String) #

Returns all enum members as an `Array(String)`.

    Color.names # => ["Red", "Green", "Blue"]

def self.values : Array(self) #

Returns all enum members as an `Array(self)`.

    Color.values # => [Color::Red, Color::Green, Color::Blue]

## Instance Method Detail

def &(other : self) #

Returns the enum member that results from applying a logical "and" operation between this enum member's value and other. This is mostly useful with flag enums.

    (IOMode::Read | IOMode::Async) & IOMode::Read # => IOMode::Read

def +(other : Int) #

Returns the enum member that results from adding other to this enum member's value.

    Color::Red + 1 # => Color::Green
    Color::Red + 2 # => Color::Blue
    Color::Red + 3 # => Color.new(3)

def -(other : Int) #

Returns the enum member that results from subtracting other to this enum member's value.

    Color::Blue - 1 # => Color::Green
    Color::Blue - 2 # => Color::Red
    Color::Blue - 3 # => Color.new(-1)

def <=>(other : self) #

Compares this enum member against another, according to their underlying value.

    Color::Red <=> Color::Blue  # => -1
    Color::Blue <=> Color::Red  # => 1
    Color::Blue <=> Color::Blue # => 0

def ==(other : self) #

Returns `true` if this enum member and other have the same underlying value.

    Color::Red == Color::Red  # => true
    Color::Red == Color::Blue # => false

def ^(other : self) #

Returns the enum member that results from applying a logical "xor" operation between this enum member's value and other. This is mostly useful with flag enums.

def clone #
def each(&block) #

Iterates each values in a Flags Enum.

    (IOMode::Read | IOMode::Async).each do |member, value|
      # yield IOMode::Read, 1
      # yield IOMode::Async, 3
    end

def hash #

Returns a hash value. This is the hash of the underlying value.

def includes?(other : self) #

Returns `true` if this enum member's value includes other. This performs a logical "and" between this enum member's value and other's, so instead of writing:

    (member & value) != 0

you can write:

    member.includes?(value)

The above is mostly useful with flag enums.

For example:

    mode = IOMode::Read | IOMode::Write
    mode.includes?(IOMode::Read)  # => true
    mode.includes?(IOMode::Async) # => false

def to_f32 : Float32 #

Returns the value of this enum member as a `Float32`

def to_f64 : Float64 #

Returns the value of this enum member as a `Float64`

def to_i : Int32 #

Returns the value of this enum member as an `Int32`.

    Color::Blue.to_i                    # => 2
    (IOMode::Read | IOMode::Write).to_i # => 3

    Color.new(10).to_i # => 10

def to_i16 : Int16 #

Returns the value of this enum member as a `Int16`

def to_i32 : Int32 #

Returns the value of this enum member as a `Int32`

def to_i64 : Int64 #

Returns the value of this enum member as a `Int64`

def to_i8 : Int8 #

Returns the value of this enum member as a `Int8`

def to_json(json : JSON::Builder) #
def to_s(io : IO) : Nil #

Appends a `String` representation of this enum member to the given io.

See also: `#to_s`.

def to_s : String #

Returns a `String` representation of this enum member. In the case of regular enums, this is just the name of the member. In the case of flag enums, it's the names joined by commas, or "None", if the value is zero.

If an enum's value doesn't match a member's value, the raw value is returned as a string.

    Color::Red.to_s                     # => "Red"
    IOMode::None.to_s                   # => "None"
    (IOMode::Read | IOMode::Write).to_s # => "Read | Write"

    Color.new(10).to_s # => "10"

def to_u16 : UInt16 #

Returns the value of this enum member as a `UInt16`

def to_u32 : UInt32 #

Returns the value of this enum member as a `UInt32`

def to_u64 : UInt64 #

Returns the value of this enum member as a `UInt64`

def to_u8 : UInt8 #

Returns the value of this enum member as a `UInt8`

def to_yaml(yaml : YAML::Builder) #
def |(other : self) #

Returns the enum member that results from applying a logical "or" operation between this enum member's value and other. This is mostly useful with flag enums.

    (IOMode::Read | IOMode::Async) # => IOMode::Read | IOMode::Async

def ~ #

Returns the enum member that results from applying a logical "not" operation of this enum member's value.

## Macro Detail

macro flags(*values) #

Convenience macro to create a combined enum (combines given members using `#|` (or) logical operator)

    IOMode.flags(Read, Write) # => IOMode::Read | IOMode::Write