Customised serialisation - sometimes

Hi

I’m using Marshal to serialise objects, and I’m defining a custom
serialisation strategy for a class. However, I want the class to still
be able to use the default serialisation provided by Marshal under
some circumstances.

My first (stupid) thought was to re-call Marshal.dump(self) in my
custom _dump method. This just causes a recursion error.

Then I thought of calling super() in the custom _dump method, but I
get the error “no superclass method: _dump”, so I guess there isn’t an
underlying _dump method in Object.

Finally, I got a bit further by removing the custom _dump method
before re-calling Marshal.dump(self). However, having removed it, and
got the default serialisation of my object, I don’t know how to put
the method back, and my class is broken:

def _dump(depth)
    if depth < -2
        return stub() # a method that returns a serialised stub
    end

    self.class.class_eval { undef_method :_dump }
    serial = Marshal.dump(self) # use default serialisation
    ???? >> how do i restore this :_dump method now?
    
    return serial
end

Thanks for any help
Alex Fenton

···

__
alex@pressure.to

Hello

Well, you could alias the method before you undef_method it:

alias :old_dump :_dump

then alias it back to _dump when you’re done.

I think this only changes it for the current object, and that to change it
for the whole class, you would need something like:

self.class.class_eval { alias_method :old_dump :_dump }

Hope that helps,

Chris

···

----- Original Message -----
From: “Alex Fenton” alex@pressure.to
Newsgroups: comp.lang.ruby
Sent: Wednesday, January 15, 2003 3:33 AM
Subject: customised serialisation - sometimes

Hi

I’m using Marshal to serialise objects, and I’m defining a custom
serialisation strategy for a class. However, I want the class to still
be able to use the default serialisation provided by Marshal under
some circumstances.

My first (stupid) thought was to re-call Marshal.dump(self) in my
custom _dump method. This just causes a recursion error.

Then I thought of calling super() in the custom _dump method, but I
get the error “no superclass method: _dump”, so I guess there isn’t an
underlying _dump method in Object.

Finally, I got a bit further by removing the custom _dump method
before re-calling Marshal.dump(self). However, having removed it, and
got the default serialisation of my object, I don’t know how to put
the method back, and my class is broken:

def _dump(depth)
    if depth < -2
        return stub() # a method that returns a serialised stub
    end

    self.class.class_eval { undef_method :_dump }
    serial = Marshal.dump(self) # use default serialisation
    ???? >> how do i restore this :_dump method now?

    return serial
end

Thanks for any help
Alex Fenton
__
alex@pressure.to