Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save e-jambon/21cb3e558082c130b47a6089056610d0 to your computer and use it in GitHub Desktop.
Save e-jambon/21cb3e558082c130b47a6089056610d0 to your computer and use it in GitHub Desktop.

Revisions

  1. @iboard iboard revised this gist May 25, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ruby-destructor-example.rb
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ def self.finalize(bar)

    end

    .

    f=Foo.new
    puts "Foo.bar is #{f.bar} now"
    f=nil
    @@ -22,4 +22,4 @@ def self.finalize(bar)
    GC.start
    sleep 1 # make sure you will see the message
    # before ruby quits
    puts "done"
    puts "done"
  2. @iboard iboard revised this gist May 25, 2013. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions ruby-destructor-example.rb
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,15 @@ def self.finalize(bar)

    end


    .
    f=Foo.new
    puts "Do something"
    puts "f destroyed"
    puts "starting GC"
    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
    sleep 1 # make sure you will see the message
    # before ruby quits
    puts "done"
  3. @iboard iboard created this gist May 25, 2013.
    22 changes: 22 additions & 0 deletions ruby-destructor-example.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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 "Do something"
    puts "f destroyed"
    puts "starting GC"
    f=nil
    GC.start
    sleep 1
    puts "done"