abstract class Class

Defined in:

class.cr

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from struct Value

! !, ==(other) ==, nil? nil?

Instance methods inherited from class Object

! !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) from_json, from_yaml(string : String) from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s

Class Method Detail

def self.==(other : Class) #

[View source]
def self.===(other) #

[View source]
def self.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](//crystal-lang.org/docs/syntax_and_semantics/as.html) for more information.

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

[View source]
def self.from_json(string_or_io) #

[View source]
def self.from_yaml(string : String) #

[View source]
def self.hash #

[View source]
def self.inspect(io) #

[View source]
def self.name : String #

Returns the name of this class.

String.name # => "String"

[View source]
def self.to_s(io) #

[View source]

Instance Method Detail

def ==(other : Class) #

[View source]
def ===(other) #

[View source]
def 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](//crystal-lang.org/docs/syntax_and_semantics/as.html) for more information.

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

[View source]
def from_json(string_or_io) #

[View source]
def from_yaml(string : String) #

[View source]
def hash #

[View source]
def inspect(io) #

[View source]
def name : String #

Returns the name of this class.

String.name # => "String"

[View source]
def to_s(io) #

[View source]

Macro Detail

macro def_equals(*fields) #

Defines an #== method by comparing the given fields.

The generated #== method has a self restriction.

class Person
  def initialize(@name, @age)
  end

  # Define a `==` method that compares @name and @age
  def_equals @name, @age
end

[View source]
macro def_equals_and_hash(*fields) #

Defines #hash and #== method from the given fields.

The generated #== method has a self restriction.

class Person
  def initialize(@name, @age)
  end

  # Define a hash method based on @name and @age
  # Define a `==` method that compares @name and @age
  def_equals_and_hash @name, @age
end

[View source]
macro def_hash(*fields) #

Defines a #hash method computed from the given fields.

class Person
  def initialize(@name, @age)
  end

  # Define a hash method based on @name and @age
  def_hash @name, @age
end

[View source]
macro delegate(method, *other_methods, to_object) #

Delegate methods to to_object.

Syntax is: delegate method1, [method2, ..., ], to_object

Note that due to current language limitations this is only useful when neither named arguments nor blocks are involved.

class StringWrapper
  def initialize(@string)
  end

  delegate downcase, @string
  delegate gsub, @string
  delegate empty?, capitalize, @string
end

wrapper = StringWrapper.new "HELLO"
wrapper.downcase       # => "hello"
wrapper.gsub(/E/, "A") # => "HALLO"
wrapper.empty?         # => false
wrapper.capitalize     # => "Hello"

[View source]
macro forward_missing_to(delegate) #

Forwards missing methods to delegate.

class StringWrapper
  def initialize(@string)
  end

  forward_missing_to @string
end

wrapper = StringWrapper.new "HELLO"
wrapper.downcase       # => "hello"
wrapper.gsub(/E/, "A") # => "HALLO"

[View source]
macro getter(*names) #

Defines getter methods for each of the given arguments.

Writing:

class Person
  getter name
end

Is the same as writing:

class Person
  def name
    @name
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  getter :name, "age"
end

[View source]
macro getter!(*names) #

Defines raise-on-nil and nilable getter methods for each of the given arguments.

Writing:

class Person
  getter! name
end

Is the same as writing:

class Person
  def name?
    @name
  end

  def name
    @name.not_nil!
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  getter! :name, "age"
end

[View source]
macro getter?(*names) #

Defines query getter methods for each of the given arguments.

Writing:

class Person
  getter? name
end

Is the same as writing:

class Person
  def name?
    @name
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  getter? :name, "age"
end

[View source]
macro property(*names) #

Defines property methods for each of the given arguments.

Writing:

class Person
  property name
end

Is the same as writing:

class Person
  def name=(@name)
  end

  def name
    @name
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  property :name, "age"
end

[View source]
macro property!(*names) #

Defines raise-on-nil property methods for each of the given arguments.

Writing:

class Person
  property! name
end

Is the same as writing:

class Person
  def name=(@name)
  end

  def name?
    @name
  end

  def name
    @name.not_nil!
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  property! :name, "age"
end

[View source]
macro property?(*names) #

Defines query property methods for each of the given arguments.

Writing:

class Person
  property? name
end

Is the same as writing:

class Person
  def name=(@name)
  end

  def name?
    @name
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  property? :name, "age"
end

[View source]
macro setter(*names) #

Defines setter methods for each of the given arguments.

Writing:

class Person
  setter name
end

Is the same as writing:

class Person
  def name=(@name)
  end
end

The arguments can be string literals, symbol literals or plain names:

class Person
  setter :name, "age"
end

[View source]