Skip to content

Instantly share code, notes, and snippets.

@iboard
Last active February 12, 2022 13:54
Show Gist options
  • Save iboard/5648299 to your computer and use it in GitHub Desktop.
Save iboard/5648299 to your computer and use it in GitHub Desktop.
Ruby 'Destructor' example.
class Foo
attr_reader :bar
def initialize
@bar = 123
ObjectSpace.define_finalizer( self, self.class.finalize(bar) )
end
def self.finalize(bar)
proc { puts "DESTROY OBJECT #{bar}" }
end
end
f=Foo.new
puts "Foo.bar is #{f.bar} now"
f=nil
# Force ruby to start the Garbage Collector
# In a real program you don't have to do this
# ruby will run the GC automatically.
GC.start
sleep 1 # make sure you will see the message
# before ruby quits
puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment