[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Marshal Clean Up Callback?

Norman Elton

3/27/2008 2:23:00 PM

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
--
Posted via http://www.ruby-....

1 Answer

ts

3/27/2008 2:35:00 PM

0

Norman Elton 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