# Marshal Clean Up Callback?

**URL:** <https://www.ruby-forum.com/t/marshal-clean-up-callback/133082>\
**Category:** Ruby\
**Created:** [March 27, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/marshal-clean-up-callback/133082 "2008-03-27T15:23:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![normelton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@normelton](https://www.ruby-forum.com/u/normelton)\
**Post date:** [March 27, 2008, 3:23pm UTC](https://www.ruby-forum.com/t/marshal-clean-up-callback/133082/1 "2008-03-27T15:23:10Z")

</div>

I’m marshal’ing an object that contains a UDP socket. Of course,  
Marshal.dump complains bitterly. Is there a way to automagically  
clean-up any un-marshal-able attributes prior to calling Marshal.dump?

Ideally, I could define an attribute, say, pre\_marshal\_dump and  
post\_marshal\_dump, that are called before and after serializing the  
object.

I know I can use marshal\_dump and marshal\_load, but then I’ve got to do  
all the serialization myself. The Marshal class does a good job of this,  
I just want to clean things up a bit before the Marshal code takes over.

Any ideas?

Thanks!

Norman

---

<div class="post-metadata">

**Author:** ![normelton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@normelton](https://www.ruby-forum.com/u/normelton)\
**Post date:** [March 27, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/marshal-clean-up-callback/133082/2 "2008-03-27T15:35:31Z")

</div>

Norman E. wrote:

> I know I can use marshal\_dump and marshal\_load, but then I’ve got to do  
> all the serialization myself. The Marshal class does a good job of this,  
> I just want to clean things up a bit before the Marshal code takes over.

No, you don’t need to make the serialization with  
marshal\_load/marshal\_dump, here an example

vgs% cat b.rb  
#!/usr/bin/ruby  
require ‘socket’

class A  
def initialize  
@a = UDPSocket.new  
@b = {1 =\> 2}  
@c = [3, 4]  
end

def marshal\_dump  
[@b, @c]  
end

def marshal\_load(x)  
@a = UDPSocket.new  
@b, @c = x  
end  
end

p Marshal.load(Marshal.dump(A.new))  
vgs%

vgs% ./b.rb  
#\<A:0xb7ca06cc @c=[3, 4], @b={1=\>2}, @a=#UDPSocket:0xb7ca0668\>  
vgs%

Guy Decoux
