Create a gist now

Instantly share code, notes, and snippets.

# normal thread.rb Queue
q =
wr = nil
# use block to ensure the reference goes away
tap do
obj =
wr =, 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'
# run GC, presumably triggering finalizer insert into queue
# reference is dead at this point
puts wr.__getobj__ rescue nil
# queue now contains the weakref object
puts q.pop(true).class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment