class WeakRef

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

A WeakRef may be used exactly like the object it references.


foo =            # create a new object instance
p foo.to_s                  # original's class
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)

Public Class Methods

new(orig) click to toggle source

Creates a weak reference to orig

Raises an ArgumentError if the given orig is immutable, such as Symbol, Integer, or Float.

Calls superclass method
# File lib/weakref.rb, line 35
def initialize(orig)
  case orig
  when true, false, nil
    @delegate_sd_obj = orig
    @@__map[self] = orig

Public Instance Methods

weakref_alive?() click to toggle source

Returns true if the referenced object is still alive.

# File lib/weakref.rb, line 56
def weakref_alive?
  @@__map.key?(self) or defined?(@delegate_sd_obj)