From: ts Date: 2003-07-08T22:18:01+09:00 Subject: Re: Marshaling objects partially >>>>> "R" == Ronald Pijnacker writes: R> On second tought... it does not seem to work fine for cycles. The R> following example breaks down with a SystemStackError: Well, you must do like marshal. Try something like this (*not tested*) svg% cat b.rb #!/usr/bin/ruby class Foo attr_accessor :bar @@dumped = {} @@loaded = [] @@num = 0 def _dump(limit) if @@dumped[self] "\000#{@@dumped[self]}\000" else @@dumped[self] = @@num @@num += 1 Marshal.dump([@bar], limit-1) end end def self._load(str) if /\A\000(\d+)\000\z/ =~ str obj = @@loaded[$1.to_i] else obj = self.new @@loaded.push(obj) obj.bar = Marshal.load(str).shift end obj end end foo1 = Foo.new foo2 = Foo.new foo1.bar = foo2 foo2.bar = foo1 p foo1 p Marshal.load(Marshal.dump(foo1)) svg% svg% b.rb #>> #>> svg% The string "\000#{@@dumped[self]}\000" must be something never used by marshal Guy Decoux