In Files

  • weakref.rb

Parent

Namespace

Class/Module Index [+]

Quicksearch

WeakRef

WeakRef is a class to represent a reference to an object that is not seen by the tracing phase of the garbage collector. This allows the referenced object to be garbage collected as if nothing is referring to it. Because WeakRef delegates method calls to the referenced object, it may be used in place of that object, i.e. it is of the same duck type.

Usage:

foo = Object.new
foo = Object.new
p foo.to_s                  # original's class
foo = WeakRef.new(foo)
p foo.to_s                  # should be same class
ObjectSpace.garbage_collect
p foo.to_s                  # should raise exception (recycled)

Public Class Methods

new(orig) click to toggle source

Create a new WeakRef from orig.

 
               # File weakref.rb, line 49
def initialize(orig)
  super
  __setobj__(orig)
end
            

Public Instance Methods

__getobj__() click to toggle source

Return the object this WeakRef references. Raises RefError if the object has been garbage collected. The object returned is the object to which method calls are delegated (see Delegator).

 
               # File weakref.rb, line 57
def __getobj__
  unless @@id_rev_map[self.__id__] == @__id
    raise RefError, "Illegal Reference - probably recycled", caller(2)
  end
  begin
    ObjectSpace._id2ref(@__id)
  rescue RangeError
    raise RefError, "Illegal Reference - probably recycled", caller(2)
  end
end
            
__setobj__(obj) click to toggle source
 
               # File weakref.rb, line 68
def __setobj__(obj)
  @__id = obj.__id__
  __old_status = Thread.critical
  begin
    Thread.critical = true
    unless @@id_rev_map.key?(self)
      ObjectSpace.define_finalizer obj, @@final
      ObjectSpace.define_finalizer self, @@final
    end
    @@id_map[@__id] = [] unless @@id_map[@__id]
  ensure
    Thread.critical = __old_status
  end
  @@id_map[@__id].push self.__id__
  @@id_rev_map[self.__id__] = @__id
end
            
weakref_alive?() click to toggle source

Returns true if the referenced object still exists, and false if it has been garbage collected.

 
               # File weakref.rb, line 87
def weakref_alive?
  @@id_rev_map[self.__id__] == @__id
end