as per the link provided by @RubyLovely: WeakRef.
I put together the following sample class:
require 'weakref'
class Foo
#block to excecute on GC.start...
FINALIZER = lambda { |object_id| p "finalizing %d" % object_id; @@no_foo -=1 }
@@no_foo=0
def initialize
@@no_foo+=1
#Initialising the finalizer...
ObjectSpace.define_finalizer(self, FINALIZER)
end
def Foo.no_foo
@@no_foo
end
end
foo = Foo.new
foo = WeakRef.new(foo)
puts Foo.no_foo
GC.start
puts Foo.no_foo