mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
65a5162550
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
41 lines
940 B
Ruby
41 lines
940 B
Ruby
#
|
|
# $Id$
|
|
# Copyright (C) 1998 Yukihiro Matsumoto. All rights reserved.
|
|
|
|
# The ObjectSpace extension:
|
|
#
|
|
# ObjectSpace.define_finalizer(obj, proc=lambda())
|
|
#
|
|
# Defines the finalizer for the specified object.
|
|
#
|
|
# ObjectSpace.undefine_finalizer(obj)
|
|
#
|
|
# Removes the finalizers for the object. If multiple finalizers are
|
|
# defined for the object, all finalizers will be removed.
|
|
#
|
|
|
|
module ObjectSpace
|
|
Finalizers = {}
|
|
def define_finalizer(obj, proc=lambda())
|
|
ObjectSpace.call_finalizer(obj)
|
|
if assoc = Finalizers[obj.id]
|
|
assoc.push(proc)
|
|
else
|
|
Finalizers[obj.id] = [proc]
|
|
end
|
|
end
|
|
def undefine_finalizer(obj)
|
|
Finalizers.delete(obj.id)
|
|
end
|
|
module_function :define_finalizer, :undefine_finalizer
|
|
|
|
Generic_Finalizer = proc {|id|
|
|
if Finalizers.key? id
|
|
for proc in Finalizers[id]
|
|
proc.call(id)
|
|
end
|
|
Finalizers.delete(id)
|
|
end
|
|
}
|
|
add_finalizer Generic_Finalizer
|
|
end
|