From: ahoward Date: 2003-06-01T01:38:06+09:00 Subject: Re: Marshaling On Sat, 31 May 2003, Dominik Werder wrote: > Hi there, > > I need to dump some objects but they have references to files. These > references are not important and could be discarded before serialization so > that no TypeError raises. But if I write my _dump method to do this, I need > to make the complete serialization on my own :((( > Is there a way to mark variables not to be marshaled? > How do you solve this problem? i *just* had this same problem yesterday... here's the two approaches i figured out, kind of hacky, but they work: /usr/home/howardat/eg/ruby > cat marshal.rb #!/usr/bin/env ruby class Foo attr_accessor :io attr_accessor :p attr_accessor :m def initialize p=__FILE__, m='w'; @io = open (@p=p), (@m=m); end end f = Foo.new f.io = nil p Marshal.load Marshal.dump f class Bar < Foo attr_accessor :args def initialize(*args) @args = args super *args end def _dump d Marshal.dump args end class << self def _load s new *(Marshal.load s) end end end b = Bar.new __FILE__, 'w' p Marshal.load Marshal.dump b /usr/home/howardat/eg/ruby > ruby marshal.rb # #, "w"], @args=["marshal.rb", "w"], @m="w", @p="marshal.rb"> the first method 'marks' vars not to be dumped with nil, the other remembers the args passed to initialize and simply re-creates the object. note that you cannot (easily) use _dump() to do the first since def _dump depth @dont_dump = nil Marshal.dump self end will recurse. an argument to Marshal.dump, which meant 'mark anything which cannot be dumped as nil' would be most useful IMHO... -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 | ~ > ruby -e 'p % ^) .intern' ====================================