mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3f3bcc697e
commit
23dd3051c0
7 changed files with 129 additions and 64 deletions
39
lib/final.rb
39
lib/final.rb
|
@ -1,41 +1,4 @@
|
|||
#
|
||||
# $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
|
||||
# final.rb is integrated into ObjectSpace; no longer needed.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue