From: Danny Sofer Date: 2001-02-06T01:40:02+09:00 Subject: [ruby-talk:10375] Problems creating persistent objects Here's the synopsis: module Storage def store puts "should be storing #{@f}" File.open(@f, "w") { |f| Marshal.dump(@data, f) } end def retrieve File.open(@f) { |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 = retrieve end puts "Old value : #{@data['m']}" @data['m'] = m puts "New value : #{@data['m']}" ObjectSpace.define_finalizer(self, proc { store } ) end end o = Obj.new('.test', 'hello') I am attempting to create persistent objects, which automatically load themselves (if there's a previously stored version) on creation and store themselves before they die. Two problems: 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? 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? Finally, I am new to ruby and have just been blown away by it. i can't remember the last time i was so excited about programming. many thanks, danny.