1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
require 'weakref'
module Ref
class WeakReference < Reference
# This implementation of a weak reference simply wraps the standard WeakRef implementation
# that comes with the Ruby standard library.
def initialize(obj) #:nodoc:
@referenced_object_id = obj.__id__
@ref = ::WeakRef.new(obj)
end
def object #:nodoc:
@ref.__getobj__
rescue => e
# Jruby implementation uses RefError while MRI uses WeakRef::RefError
if (defined?(RefError) && e.is_a?(RefError)) || (defined?(::WeakRef::RefError) && e.is_a?(::WeakRef::RefError))
nil
else
raise e
end
end
end
end
|