Ok, I spent some time today reading various chains on destructor
questions and grabage collection and I am not sure I understood
it all. So I put this example together to see if anyone can tell me if
using the Class name as the first parameter to define_finalizer is
the proper thing to do. It seems to work quite well in simulating a
destructor. I even threw example 2 together to answer Joseph's
question. This seemed like as good a place for this as others ...
Example 1:
class Preferences < Hash
def initialize(preferenceFile)
@preferenceFile = preferenceFile
load
ObjectSpace.define_finalizer(Preferences, proc{save})
end
def load
puts "Loading file: %s" % @preferenceFile
end
def save
p self
p length
end
end
p1 = Preferences.new('File1')
p2 = Preferences.new('File2')
p1['P1'] = 'Test-1'
p2['P2'] = 'Test-2'
p2['P3'] = 'Test-3'
Example 2:
class A
def finalizer
Proc.new{ write }
end
def initialize
ObjectSpace.define_finalizer(A, finalizer)
end
def write
puts "doing the final write"
end
end
a = A.new