From: Avi Bryant Date: 2001-02-06T08:29:47+09:00 Subject: [ruby-talk:10389] Re: Problems creating persistent objects On Tue, 6 Feb 2001, Danny Sofer wrote: > 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='') > 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 something like module Storage def new(f) if File.exists?(f) File.open(f) {|f| return Marshal.load(f) } else super end end end class Obj extend Storage ... end (me, I miss the semantics of [[Cls alloc] init] in Obj-C...)