From: Danny Sofer Date: 2001-02-07T01:40:02+09:00 Subject: [ruby-talk:10430] Re: Problems creating persistent objects "Danny Sofer" wrote > Here's the synopsis: > Thanks for the responses. they were very helpful. there follows an amended code snippet. 'Storage' can be used with any class that includes and extends it. the only requirement is that there be a publically readable instance variable called f, that is the first argument of any call to 'new'. ObjectSpace.each_object is used to pick up the object and save it before it gets destroyed. this seems a bit of a kludge, but it's the only way i could work out to reference the object. is there a better way? module Storage def new(f, *args) ObjectSpace.define_finalizer(self, proc {finalizer(f)} ) if File.exists?(f) File.open(f) {|fl| return Marshal.load(fl) } else super end end def finalizer(f) ObjectSpace.each_object(Storage) do |s| puts s.inspect if s.f == f then File.open(f, "w") {|fl| Marshal.dump(s, fl)} end end end end class Obj include Storage # mix in Storage extend Storage # make class methods (i.e. new) available attr_reader :f def initialize(f, m='') @f = f @m = m end end o = Obj.new('.test', 'hello' ) thanks again for your time and bandwidth. danny