0001-weakref.rb-non-delegation.patch

Nobuyoshi Nakada, 10/31/2013 02:07 PM

Download (1.63 KB)

View differences:

lib/weakref.rb
73 73

  
74 74
  @@__map = ::ObjectSpace::WeakMap.new
75 75

  
76
  class << (@@__wrapper = Object.new) # :nodoc:
77
    def inspect
78
      obj = get
79
      "#<WeakRef:#{obj ? obj.inspect : 'dead'}>"
80
    end
81

  
82
    def get
83
      @@__map[self]
84
    end
85

  
86
    def weakref_alive?
87
      !!@@__map[self]
88
    end
89

  
90
    freeze
91
  end
92

  
93
  @@__const_wrapper = Struct.new(:get) # :nodoc:
94
  @@__const_wrapper.class_eval do
95
    def weakref_alive?
96
      true
97
    end
98
  end
99

  
100
  def self.[](obj)
101
    case obj
102
    when true, false, nil, ::Symbol, ::Fixnum, ::Float
103
      @@__const_wrapper.new(obj)
104
    else
105
      @@__map[w = @@__wrapper.clone] = obj
106
      w
107
    end
108
  end
109

  
110
  def self.new(*)
111
    if $VERBOSE
112
      label = caller_locations(1, 1)[0].to_s.freeze
113
      (@__warned ||= {})[label] ||=
114
        begin
115
          warn <<-MSG
116
#{label}: WeakRef.new with delegation is deprecated, use WeakRef[]
117
#{label}: instead and use `get' method to achieve the target object.
118
          MSG
119
          true
120
        end
121
    end
122
    super
123
  end
124

  
76 125
  ##
77 126
  # Creates a weak reference to +orig+
78 127
  #
79
-