WeakRef(T)
Weak Reference class that allows a referenced object to be garbage-collected.
WARNING: The referenced object cannot be a module.
NOTE: To use WeakRef, you must explicitly import it with require "weak_ref"
require "weak_ref"
ref = WeakRef.new("oof".reverse)
p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void)@0x7f83406eafc0>
GC.collect
p ref # => #<WeakRef(String):0x7f83406eafa0 @target=Pointer(Void).null>
p ref.value # => nil
Note that the collection of objects is not deterministic, and depends on many subtle aspects. For instance,
if the example above is modified to print ref.value in the first print, then the collector will not collect it.
Constructors
new(target : T)
Source