Skip to content

class WeakRef(T)
inherits Reference

Weak Reference class that allows a referenced object to be garbage-collected.

require "weak_ref"

ref = WeakRef.new("oof".reverse)
p ref.value # => "foo"
GC.collect
p ref.value # => nil

Class methods

.new(target : T)

View source

Methods

#value

Returns the referenced object or Nil if it has been garbage-collected.

View source