# frozen_string_literal: true
# Weak Reference class that allows a referenced object to be
# A WeakRef may be used exactly like the object it references.
# foo = Object.new # create a new object instance
# p foo.to_s # original's class
# foo = WeakRef.new(foo) # reassign foo with WeakRef instance
# p foo.to_s # should be same class
# GC.start # start the garbage collector
# p foo.to_s # should raise exception (recycled)
class WeakRef < Delegator
# RefError is raised when a referenced object has been recycled by the
class RefError < StandardError
@@__map = ::ObjectSpace::WeakMap.new
# Creates a weak reference to +orig+
# Raises an ArgumentError if the given +orig+ is immutable, such as Symbol,
@@__map[self] or defined?(@delegate_sd_obj) ? @delegate_sd_obj :
Kernel::raise(RefError, "Invalid Reference - probably recycled", Kernel::caller(2))
def __setobj__(obj) # :nodoc:
# Returns true if the referenced object is still alive.
@@__map.key?(self) or defined?(@delegate_sd_obj)