W3cubDocs

/Crystal 0.31

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:

primitives.cr
reference.cr
json/any.cr
yaml/any.cr

Constructors

Instance Method Summary

Constructor Detail

def self.newSource

Instance Method Detail

def ==(other : self)Source

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

def ==(other : JSON::Any)Source

def ==(other : YAML::Any)Source

def ==(other)Source

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

def dupSource

Returns a shallow copy of this object.

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

def hash(hasher)Source

def inspect(io : IO) : NilSource

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
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>

def object_id : UInt64Source

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 = pointer.as(String)
string2.object_id == string.object_id # => true

def pretty_print(pp) : NilSource

def same?(other : Reference)Source

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.

def same?(other : Nil)Source

Returns false: a reference is never nil.

def to_s(io : IO) : NilSource

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
end

Person.new("John", 32).to_s # => #<Person:0x10a199f20>

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/Reference.html