Marshaling 
Author Message
 Marshaling

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?

thanks!
Dominik



Wed, 16 Nov 2005 20:33:29 GMT  
 Marshaling

Quote:

> 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

end

f = Foo.new
f.io = nil
p Marshal.load Marshal.dump f

class Bar < Foo
  attr_accessor :args
  def initialize(*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


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

    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

  | Phone:  303-497-7238
  | Fax:    303-497-7259
  | ~ > ruby -e 'p % ^) .intern'
  ====================================



Thu, 17 Nov 2005 00:10:58 GMT  
 Marshaling

Quote:

> 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?

> thanks!
> Dominik

You could try getting rid of the TypeError like this:

class File
  def _dump(depth)
    ""
  end
  class <<self
    def _load(str)
      nil
    end
  end
end

Stephen.
+- S.D.Sykes - www.stephensykes.com - +358 50 577 3879 -
ruby -e '_=2;"||_25~!A59(1A~47!K~#-6<.A~".scan(/./){
putc$&>$`?$/:_+=$&[0]-?4}'



Fri, 18 Nov 2005 20:10:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. marshal module in jython does not marshal code!

2. Marshal bug?

3. Marshal bug

4. Marshal string format

5. Roundoff problem with Float and Marshal

6. 1.8 marshal.c compatible with 1.6?

7. Marshal.dump() taking a long time

8. Cyclic Marshals :: again the need for a built in creation parent method

9. poor performance of Marshal.dump on Hashes

10. go ahead, marshal ruby objects via html

11. Marshaling with ARGF

12. Ruby Segfault: Marshaling large objects

 

 
Powered by phpBB® Forum Software