class

Class

Inherits Value / Object

Constructors

cast(other) : self

Casts other to this class.

This is the same as using as, but allows the class to be passed around as an argument. See the documentation on as for more information.

klass = Int32
number = [99, "str"][0]
typeof(number)             # => (String | Int32)
typeof(klass.cast(number)) # => Int32
Source

Class methods

<(other : T.class) : Bool forall T

Returns whether this class inherits or includes other.

Int32 < Number  # => true
Int32 < Value   # => true
Int32 < Int32   # => false
Int32 <= String # => false
Source
<=(other : T.class) : Bool forall T

Returns whether this class inherits or includes other, or is equal to other.

Int32 < Number  # => true
Int32 < Value   # => true
Int32 <= Int32  # => true
Int32 <= String # => false
Source
==(other : Class) : Bool

Returns whether this class is the same as other.

Int32 == Int32  # => true
Int32 == String # => false
Source
===(other)

Case equality.

The === method is used in a case ... when ... end expression.

For example, this code:

case value
when x
  # something when x
when y
  # something when y
end

Is equivalent to this code:

if x === value
  # something when x
elsif y === value
  # something when y
end

Object simply implements === by invoking ==, but subclasses (notably Regex) can override it to provide meaningful case-equality semantics.

Source
>(other : T.class) : Bool forall T

Returns whether other inherits or includes self.

Number > Int32  # => true
Number > Number # => false
Number > Object # => false
Source
>=(other : T.class) forall T

Returns whether other inherits or includes self, or is equal to self.

Number >= Int32  # => true
Number >= Number # => true
Number >= Object # => false
Source
|(other : U.class) forall U

Returns the union type of self and other.

Int32 | Char # => (Int32 | Char)
Source
clone
Source
dup

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.

Source
hash(hasher)

See Object#hash(hasher)

Source
inspect(io : IO) : Nil

Prints to io an unambiguous and information-rich string representation of this object, typically intended for developers.

It is similar to #to_s(IO), but often provides more information. Ideally, it should contain sufficient information to be able to recreate an object with the same value (given an identical environment).

For types that don't provide a custom implementation of this method, default implementation delegates to #to_s(IO). This said, it is advisable to have an appropriate #inspect implementation on every type. Default implementations are provided by Struct#inspect and Reference#inspect.

::p and ::p! use this method to print an object in STDOUT.

Source
name

Returns the name of this class.

String.name # => "String"
Source
nilable?

Returns true if nil is an instance of this type.

Int32.nilable?            # => false
Nil.nilable?              # => true
(Int32 | String).nilable? # => false
(Int32 | Nil).nilable?    # => true
NoReturn.nilable?         # => false
Value.nilable?            # => true
Source
to_s(io : IO) : Nil

Prints a nicely readable and concise string representation of this object, typically intended for users, to io.

This method is called when an object is interpolated in a string literal:

"foo #{bar} baz" # calls bar.to_io with the builder for this string

IO#<< calls this method to append an object to itself:

io << bar # calls bar.to_s(io)

Thus implementations must not interpolate self in a string literal or call io << self which both would lead to an endless loop.

Also see #inspect(IO).

Source

Instance methods

<(other : T.class) : Bool forall T

Returns whether this class inherits or includes other.

Int32 < Number  # => true
Int32 < Value   # => true
Int32 < Int32   # => false
Int32 <= String # => false
Source
<=(other : T.class) : Bool forall T

Returns whether this class inherits or includes other, or is equal to other.

Int32 < Number  # => true
Int32 < Value   # => true
Int32 <= Int32  # => true
Int32 <= String # => false
Source
==(other : Class) : Bool

Returns whether this class is the same as other.

Int32 == Int32  # => true
Int32 == String # => false
Source
===(other)

Case equality.

The === method is used in a case ... when ... end expression.

For example, this code:

case value
when x
  # something when x
when y
  # something when y
end

Is equivalent to this code:

if x === value
  # something when x
elsif y === value
  # something when y
end

Object simply implements === by invoking ==, but subclasses (notably Regex) can override it to provide meaningful case-equality semantics.

Source
>(other : T.class) : Bool forall T

Returns whether other inherits or includes self.

Number > Int32  # => true
Number > Number # => false
Number > Object # => false
Source
>=(other : T.class) forall T

Returns whether other inherits or includes self, or is equal to self.

Number >= Int32  # => true
Number >= Number # => true
Number >= Object # => false
Source
|(other : U.class) forall U

Returns the union type of self and other.

Int32 | Char # => (Int32 | Char)
Source
cast(other) : self

Casts other to this class.

This is the same as using as, but allows the class to be passed around as an argument. See the documentation on as for more information.

klass = Int32
number = [99, "str"][0]
typeof(number)             # => (String | Int32)
typeof(klass.cast(number)) # => Int32
Source
clone
Source
dup

Returns a shallow copy of this object.

Because Value is a value type, this method returns self, which already involves a shallow copy of this object because value types are passed by value.

Source
hash(hasher)

See Object#hash(hasher)

Source
inspect(io : IO) : Nil

Prints to io an unambiguous and information-rich string representation of this object, typically intended for developers.

It is similar to #to_s(IO), but often provides more information. Ideally, it should contain sufficient information to be able to recreate an object with the same value (given an identical environment).

For types that don't provide a custom implementation of this method, default implementation delegates to #to_s(IO). This said, it is advisable to have an appropriate #inspect implementation on every type. Default implementations are provided by Struct#inspect and Reference#inspect.

::p and ::p! use this method to print an object in STDOUT.

Source
name

Returns the name of this class.

String.name # => "String"
Source
nilable?

Returns true if nil is an instance of this type.

Int32.nilable?            # => false
Nil.nilable?              # => true
(Int32 | String).nilable? # => false
(Int32 | Nil).nilable?    # => true
NoReturn.nilable?         # => false
Value.nilable?            # => true
Source
to_s(io : IO) : Nil

Prints a nicely readable and concise string representation of this object, typically intended for users, to io.

This method is called when an object is interpolated in a string literal:

"foo #{bar} baz" # calls bar.to_io with the builder for this string

IO#<< calls this method to append an object to itself:

io << bar # calls bar.to_s(io)

Thus implementations must not interpolate self in a string literal or call io << self which both would lead to an endless loop.

Also see #inspect(IO).

Source