Class: Zoidberg::WeakRef

Inherits:
BasicObject
Defined in:
lib/zoidberg/weak_ref.rb,
lib/zoidberg/weak_ref.rb

Overview

Provide weak reference to object allowing for it to be garbage collected. This is a stripped down version of the ::WeakRef class

Defined Under Namespace

Classes: RecycledException

Constant Summary collapse

@@__zoidberg_map =
::ObjectSpace::WeakMap.new

Instance Method Summary collapse

Constructor Details

#initialize(orig) ⇒ self

Create a new weak reference

Parameters:

  • orig (Object)

    referenced object



29
30
31
32
# File 'lib/zoidberg/weak_ref.rb', line 29

def initialize(orig)
  @_key = orig.object_id.to_s
  @@__zoidberg_map[@_key] = orig
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
# File 'lib/zoidberg/weak_ref.rb', line 34

def method_missing(*args, &block) # :nodoc:
  if(@@__zoidberg_map[@_key])
    @@__zoidberg_map[@_key].__send__(*args, &block)
  else
    ::Kernel.raise RecycledException.new('Instance has been recycled by the system!', @_key)
  end
end