类 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)