From: Joel VanderWerf Date: 2005-09-26T06:53:58+09:00 Subject: Re: Concerning Marshalling ts wrote: >>>>>>"c" == christophe poucet@gmail com writes: > > > c> That's why I tried something as follows: > > Perhaps best to use marshal_dump/marshal_load > > Guy Decoux That's a good suggestion. The marshal_dump/marshal_load pair can be very useful, because it integrates well with the whole Marshal.dump or Marshal.load process. An inherent problem with _dump and _load is that you have to use strings, so you are responsible for somehow preserving and restoring references to other objects that are also being dumped. This is critical when the original dump call was on an object that referred to your object, not directly to your object. With marshal_dump/marshal_load, you produce/consume an arbitrary Marshallable object, possibly containing references, and all the code in marshal.c takes over the hard task of preserving and restoring references. Probably an example will say it better: class Node attr_accessor :prev_node, :next_node attr_accessor :temp_data # don't want this to persist (maybe it's a cache, or simply # undumpable, like a proc, or a file, or a singleton). def marshal_dump dumped_obj = [prev_node, next_node] dumped_obj end def marshal_load(dumped_obj) @prev_node, @next_node = dumped_obj end end node1 = Node.new node2 = Node.new node1.next_node = node2 node2.prev_node = node1 node1.temp_data = STDOUT node2.temp_data = proc {"foo"} p node1 p Marshal.load(Marshal.dump(node1)) __END__ output: #, @temp_data=#>, @temp_data=#> #, @next_node=nil>> -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407