class Reference


Reference is the base class of classes you define in your program. It is set as a class' superclass when you don't specify one:

class MyClass # < Reference

A reference type is passed by reference: when you pass it to methods, return it from methods or assign it to variables, a pointer is actually passed.

Invoking .new on a Reference allocates a new instance on the heap. The instance's memory is automatically freed (garbage-collected) when the instance is no longer referred by any other entity in the program.

Direct Known Subclasses

Defined in:



Instance Method Summary

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
, in?(collection : Object) : Bool
in?(*values : Object) : Bool
, inspect(io : IO) : Nil
inspect : String
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
, to_s(io : IO) : Nil
to_s : String
, to_yaml(io : IO) : Nil
to_yaml : String
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io, root : String)
, from_yaml(string_or_io : String | IO) from_yaml

Macros inherited from class Object

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

Constructor Detail

def #

[View source]

Instance Method Detail

def ==(other : self) #

Returns true if this reference is the same as other. Invokes #same?.

[View source]
def ==(other : JSON::Any) #

[View source]
def ==(other : YAML::Any) #

[View source]
def ==(other) #

Returns false (other can only be a Value here).

[View source]
def dup #

Returns a shallow copy of this object.

This allocates a new object and copies the contents of self into it.

[View source]
def hash(hasher) #

[View source]
def initialize #

[View source]
def inspect(io : IO) : Nil #

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
end"John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>

[View source]
def object_id : UInt64 #

Returns a UInt64 that uniquely identifies this object.

The returned value is the memory address of this object.

string = "hello"
string.object_id # => 4460249568

pointer = Pointer(String).new(string.object_id)
string2 =
string2.object_id == string.object_id # => true

[View source]
def pretty_print(pp) : Nil #

[View source]
def same?(other : Reference) : Bool #

Returns true if this reference is the same as other. This is only true if this reference's #object_id is the same as other's.

[View source]
def same?(other : Nil) #

Returns false: a reference is never nil.

[View source]
def to_s(io : IO) : Nil #

Appends a short String representation of this object which includes its class name and its object address.

class Person
  def initialize(@name : String, @age : Int32)
end"John", 32).to_s # => #<Person:0x10a199f20>

[View source]