abstract class Class

Superclass hierarchy

Object
Value
Class

Defined in:

Class Method Summary

Instance Method Summary

Macro Summary

Class Method Detail

def self.==(other : Class)

def self.from_json(string_or_io)

def self.hash

def self.inspect(io)

def self.name : String

Returns the name of this class.

String.name #=> "String"

def self.to_s(io)

Instance Method Detail

def ==(other : Class)

def from_json(string_or_io)

def hash

def inspect(io)

def name : String

Returns the name of this class.

String.name #=> "String"

def to_s(io)

Macro Detail

macro alias_method(new_method, old_method)

Defines new_method as an alias of old_method.

This creates a new method new_method that invokes old_method.

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

class Person
  getter name

  def initialize(@name)
  end

  alias_method full_name, name
end

person = Person.new "John"
person.name #=> "John"
person.full_name #=> "John"

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

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

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

macro delegate(method, to_object)

Delegate method to 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
end

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

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"

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

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

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

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

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

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

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