public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# normal thread.rb Queue
q = Queue.new
wr = nil
 
# use block to ensure the reference goes away
tap do
obj = Object.new
wr = WeakRef.new(obj, q)
 
# reference is live at this point
puts wr.__getobj__
# queue is empty at this point
puts q.pop(true) rescue puts 'nothing in queue'
end
 
# run GC, presumably triggering finalizer insert into queue
GC.start
 
# reference is dead at this point
puts wr.__getobj__ rescue nil
 
# queue now contains the weakref object
puts q.pop(true).class

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.