File: final.rb

package info (click to toggle)
ruby 1.4.3-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,068 kB
  • ctags: 7,509
  • sloc: ansic: 60,668; ruby: 23,106; yacc: 4,122; sh: 1,753; lisp: 997; makefile: 597; sed: 68; awk: 36; tcl: 31; perl: 17; python: 6
file content (41 lines) | stat: -rw-r--r-- 986 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#
# $Id: final.rb,v 1.2 1999/08/13 05:45:16 matz Exp $
# 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