diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-01-19 05:08:25 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 1998-01-19 05:08:25 +0000 |
commit | 5c47ddf9e7fa05bc993818c166e8162c20a970f9 (patch) | |
tree | 2f6b7ca53886b3e41a06d1268fb65e0a7bfbe741 /lib/final.rb | |
parent | d556e58fc0f5d94961e4ecb765cb1052cd754871 (diff) |
Mon Jan 19 14:06:13 JST 1998
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@23 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/final.rb')
-rw-r--r-- | lib/final.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/final.rb b/lib/final.rb new file mode 100644 index 0000000000..566d1aae42 --- /dev/null +++ b/lib/final.rb @@ -0,0 +1,41 @@ +# +# $Id$ +# Copyright (C) 1998 Yukihiro Matsumoto. All rights reserved. + +# The ObjectSpace extention: +# +# 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 + Finalizer = {} + def define_finalizer(obj, proc=lambda()) + ObjectSpace.call_finalizer(obj) + if assoc = Finalizer[obj.id] + assoc.push(proc) + else + Finalizer[obj.id] = [proc] + end + end + def undefine_finalizer(obj) + Finalizer.delete(obj.id) + end + module_function :define_finalizer, :remove_finalizer + + Generic_Finalizer = proc {|id| + if Finalizer.key? id + for proc in Finalizer[id] + proc.call(id) + end + Finalizer.delete(id) + end + } + add_finalizer Generic_Finalizer +end |