abstract class Object

Overview

Object is the base type of all Crystal objects.

Included Modules

JSON::Mapping, Spec::ObjectExtensions

Direct Known Subclasses

Reference, Value

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

abstract def !

Boolean-negates this object.


def !=(other)

Returns true if this object is not equal to other.

By default this method is implemented as !(self == other) so there's no need to override this unless there's a more efficient way to do it.


abstract def <=>(other)

Comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, or a positive number otherwise.

Subclasses define this method to provide class-specific ordering.

# Sort in a descending way
[4, 7, 2].sort { |x, y| x <=> y } #=> [7, 4, 2]

abstract def ==(other)

Returns true if this object is equal to other.

Subclasses override this method to provide class-specific meaning.


def ===(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.


def =~(other)

Pattern match.

Overridden by descendants (notably Regex and String) to provide meaningful pattern-match semantics.


def clone

Returns a deep copy of this object.

Object returns self, but subclasses override this method to provide specific clone behaviour.


def colorize(fore)

def colorize

abstract def dup

Returns a shallow copy of this object. Not all objects implement this method.


abstract def hash

Generates an Int hash value for this object.

This method must have the property that a == b implies a.hash == b.hash.

The hash value is used along with #== by the Hash class to determine if two objects reference the same hash key.


def inspect(io : IO)

Appends a string representation of this object to the given IO object.

Similar to #to_s(io), but usually appends more information about this object.


def inspect

Returns a String representation of this object.

Similar to #to_s, but usually returns more information about this object.

Classes must usually not override this method. Instead, they must override #inspect(io), which must append to the given IO object.


def itself

Return self.

str = "hello"
str.itself.object_id == str.object_id #=> true

def not_nil!

Returns self. Nil overrides this method and raises an exception.


def tap(&block)

Yields self to the block, and then returns self.

The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.

(1..10)                .tap {|x| puts "original: #{x.inspect}"}
  .to_a                .tap {|x| puts "array: #{x.inspect}"}
  .select {|x| x%2==0} .tap {|x| puts "evens: #{x.inspect}"}
  .map {|x| x*x}       .tap {|x| puts "squares: #{x.inspect}"}

def to_json

def to_pretty_json

def to_pretty_json(io : IO)

abstract def to_s(io : IO)

Appends a String representation of this object to the given IO object.

An object must never append itself to the io argument, as this will in turn call #to_s(io) on it.


def to_s

Returns a string representation of this object.

Descendants must usually not override this method. Instead, they must override #to_s(io), which must append to the given IO object.


def try(&block)

Yields self. Nil overrides this method and doesn't yield.

This method is useful for dealing with nilable types, to safely perform operations only when the value is not nil.

# First program argument in downcase, or nil
ARGV[0]?.try &.downcase

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