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.
Reference
Reference
Reference
Object
Object
Object
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/WeakRef.html