类 WeakRef
弱引用类,允许被引用的对象被垃圾回收。
WeakRef
的使用方式与其引用的对象完全相同。
用法
foo = Object.new # create a new object instance p foo.to_s # original's class foo = WeakRef.new(foo) # reassign foo with WeakRef instance p foo.to_s # should be same class GC.start # start the garbage collector p foo.to_s # should raise exception (recycled)
常量
- VERSION
公共类方法
源代码
# File lib/weakref.rb, line 34 def initialize(orig) case orig when true, false, nil @delegate_sd_obj = orig else @@__map[self] = orig end super end
创建一个对 orig
的弱引用
调用父类方法
Delegator::new
公共实例方法
源代码
# File lib/weakref.rb, line 55 def weakref_alive? @@__map.key?(self) or defined?(@delegate_sd_obj) end
如果被引用的对象仍然存活,则返回 true。