Class: Ref::WeakReference

Inherits:
Reference show all
Defined in:
lib/ref/weak_reference.rb,
lib/ref/weak_reference/weak_ref.rb,
lib/ref/weak_reference/iron_ruby.rb,
lib/ref/weak_reference/pure_ruby.rb

Overview

This is a pure ruby implementation of a weak reference. It is much more efficient than the WeakRef implementation bundled in MRI 1.8 and 1.9 subclass Delegator which is very heavy to instantiate and utilizes a because it does not fair amount of memory under Ruby 1.8.

Direct Known Subclasses

Mock::MockWeakReference

Defined Under Namespace

Classes: ReferencePointer

Constant Summary collapse

@@weak_references =
{}
@@lock =
SafeMonitor.new
@@reference_finalizer =

Finalizer that cleans up weak references when references are destroyed.

lambda do |object_id|
  @@lock.synchronize do
    reference_pointer = @@weak_references.delete(object_id)
    reference_pointer.cleanup if reference_pointer
  end
end

Instance Attribute Summary

Attributes inherited from Reference

#referenced_object_id

Instance Method Summary collapse

Methods inherited from Reference

#inspect

Constructor Details

#initialize(obj) ⇒ WeakReference

Create a new weak reference to an object. The existence of the weak reference will not prevent the garbage collector from reclaiming the referenced object.



16
17
18
# File 'lib/ref/weak_reference.rb', line 16

def initialize(obj)
  raise NotImplementedError.new("This is an abstract class; you must require an implementation")
end

Instance Method Details

#objectObject

Get the reference object. If the object has already been garbage collected, then this method will return nil.



22
23
24
# File 'lib/ref/weak_reference.rb', line 22

def object
  raise NotImplementedError.new("This is an abstract class; you must require an implementation")
end