File: weak_ref.rb

package info (click to toggle)
ruby-ref 1.0.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 332 kB
  • ctags: 186
  • sloc: ruby: 1,107; java: 92; makefile: 2
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