Hi,

In message "[ruby-talk:10375] Problems creating persistent objects"
    on 01/02/06, "Danny Sofer" <danny / sofer.com> writes:

|Here's the synopsis:

|    ObjectSpace.define_finalizer(self, proc { store } )

The closure created by `proc{store}' contains the reference to self,
so that GC will never reclaim the object.  I think that's not what you
want.

I'd like to do something like this:

module Storage
  def Storage.finalizer(path, data)
    p "finalizer"
    proc{File.open(path, "w") {|f| Marshal.dump(data, f)}}
  end
  def initialize()
    ObjectSpace.define_finalizer(self, Storage.finalizer(@f, @data))
  end
  def Storage.retrieve(path)
    File.open(path) { |f| return Marshal.load(f) }
  end
end

class Obj
  include Storage

  def initialize(f, m='')
    @f = f
    puts "the file is #{@f}"
    @data = {}
    @data['m'] = ''
    if File.exists?(f)
      @data = Storage.retrieve(f)
    end
    puts "Old value : #{@data['m']}"
    @data['m'] = m
    puts "New value : #{@data['m']}"
    super(f, data)
   end
end


|1) i wanted to do something like: "self = retrieve", but in the end settled
|for putting my data in to a hash (@data) and storing that instead, because
|assigning anything to self is a bit naughty. is there a more elegant way to
|round this?

How about using something other than new?  Like Avi mentioned in [ruby-talk:10389].

|2) by the time the finalizer is called, all my instance variables seem to
|have disappeared, so i can't automatically store my data. any other
|suggestions?

It's for safety.  Try the workaround above.

							matz.