Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | ObjectSpace | define_finalizer

概要

ObjectSpace#define_finalizer(obj, proc) -> Array
ObjectSpace#define_finalizer(obj) {|id| ...} -> Array

詳細

obj が解放されるときに実行されるファイナライザ proc を 登録。
同一オブジェクトへの設定は追加になる。

サンプルコード
class Hoge
  def Hoge.callback
    proc {
      puts "finalize"
    }
  end

  def initialize
    ObjectSpace.define_finalizer(self, Hoge.callback)
  end
end

hoge = Hoge.new
GC.start

出力

finalize