File: weak_reference.rb

package info (click to toggle)
ruby-ref 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 268 kB
  • sloc: ruby: 1,262; java: 92; makefile: 5
file content (26 lines) | stat: -rw-r--r-- 905 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Ref
  # A WeakReference represents 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.
  #
  # === Example usage:
  #
  #   foo = Object.new
  #   ref = Ref::WeakReference.new(foo)
  #   ref.object			# should be foo
  #   ObjectSpace.garbage_collect
  #   ref.object			# should be nil
  class WeakReference < Reference
    
    # Create a weak reference to an object.
    def initialize(obj)
      raise NotImplementedError.new("This is an abstract class; you must require an implementation")
    end

    # Get the referenced object. If the object has been reclaimed by the
    # garbage collector, then this will return nil.
    def object
      raise NotImplementedError.new("This is an abstract class; you must require an implementation")
    end
  end
end