File: weak_ref.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 (23 lines) | stat: -rw-r--r-- 642 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
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