From: David Alan Black Date: 2002-02-17T21:49:10+09:00 Subject: Re: dump/load a class that has C and Ruby data Hello -- On Sun, 17 Feb 2002, Joel VanderWerf wrote: > > I have some classes whose instances have C data as well as Ruby attrs. > If I Marshal::dump them, I lose the C part. So I have to define _dump in > my classes. Well, it's not so hard to pack my C data up into a string. I > can even put this string in a Ruby attribute. But then how do I dump the > Ruby part? I don't really want to reimplement all the work in > Marshal::dump just for my classes. I wish there was a way to call > Marshal::dump with an argument that said "ignore the _dump". Then my > _dump could first pack the C data into an attr, and then call dump in > this way, and I'd be all set. Shouldn't Marshal::dump be more easy to > reuse? > > Anybody know a way to do this? You could put a hook into Marshal::dump, and flag the objects you wanted to receive special treatment. Adapting an idea from Dave Thomas, in the recent respond_to? thread: module Marshal class << self alias :olddump :dump def dump(obj,*args) if obj.kind_of? SpecialDumpee puts "Special treatment!" end olddump(obj,*args) end end end module SpecialDumpee end class Thing include SpecialDumpee # ... end Marshal.dump(Thing.new) # => "Special treatment!" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav