Run a Method on deletion of an Object

戏子无情 提交于 2019-12-04 18:49:53
Sergio Tulentsev

Even though ruby doesn't have explicit destructors, it has support for finalizers. Finalizer is a piece of code that gets called when your object is garbage-collected.

class Foo
  @@no_foo = 0

  def initialize
    @@no_foo += 1
    ObjectSpace.define_finalizer(self, Foo.method(:delete))
  end

  def self.delete id # also this argument seems to be necessary
    @@no_foo -= 1
  end

  def self.no_foo
    @@no_foo
  end
end

Seems that you won't be able to do instance-specific cleanup in the finalizer. It has to be completely detached from the instance in order to work correctly. Here are answer that might also help you: answer, answer.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!