>>>>> "R" == Ronald Pijnacker <rhp / dse.nl> 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
#<Foo:0x40099100 @bar=#<Foo:0x40099010 @bar=#<Foo:0x40099100 ...>>>
#<Foo:0x40098ebc @bar=#<Foo:0x40098e30 @bar=#<Foo:0x40098ebc ...>>>
svg% 


 The string "\000#{@@dumped[self]}\000" must be something never used by
 marshal 


Guy Decoux