class Reference

Overview

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
end

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:

reference.cr

Instance Method Summary

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

Instance Method Detail

def ! #

Returns false: a reference is always truthy.


[View source]
def ==(other) #

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


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

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


[View source]
def hash #

Returns this reference's #object_id as the hash value.


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

[View source]
def nil? #

Returns false: a reference is never nil.


[View source]
def object_id #

Returns a UInt64 that uniquely identifies this object.

The returned value is the memory address of this object.

ref = Reference.new
pointer = Pointer(Reference).new(ref.object_id)
ref2 = pointer as Reference
ref2.object_id == ref.object_id #=> true

def same?(other : Nil) #

Returns false: a reference is never nil.


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

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 to_s(io : IO) : Nil #

[View source]