From: Robert Klemme Date: 2008-05-23T18:40:01+09:00 Subject: Re: customized serialization unreliable???? On 23.05.2008 10:33, Andrew Mitchell wrote: >> def marshal_dump() >> return [@version,@data] >> end > > If there are actually classes built on instance methods marshal_dump and > marshal_load, then Marshal would be completely ignoring those methods. I don't think so, at least not in 1.8.6: robert@fussel /cygdrive/c/Temp $ irb irb(main):001:0> class F irb(main):002:1> def marshal_dump irb(main):003:2> puts "dump" irb(main):004:2> "123" irb(main):005:2> end irb(main):006:1> def marshal_load(x) irb(main):007:2> puts "load #{x}" irb(main):008:2> end irb(main):009:1> end => nil irb(main):010:0> Marshal.load(Marshal.dump(F.new)) dump load 123 => # irb(main):011:0> > Assuming you meant "_dump" which is the instance method to overload for > customized serialization, and either "self._load" or klassname._load > which is the class method for the same, then this would be happening: I believe this is yet another mechanism to control custom marshalling. > Array.to_s will be called in Marshal.dump because it expects a string. I don't think so. The mechanism is different. It would be too fragile to depend on #to_s to return something that can be used to deserialize. > For example, with the YYY class: > @version = 1 > @data = ["e", "i"] > _dump returns [1, ["e", "i"]] > The Marshaling process will form "1ei" > > Your Loading process with "1ei" will give > @version = 1 > @data = "e" # instead of ["e", "i"] > > > If for whatever reason you are Marshaling the results of your > marshal_dump methods -- Marshal.dump(my_yyy.marshal_dump) --, then you > have neglected to properly handle object graph cycles, and shared > references. Correct. See my other posting for a nice example. :-) All in all I believe, if one wants to exclude some fields from serialization (like with "transient" in Java) the best way is to implement #marshal_dump to just return an array of the fields that need to be serialized and deserialized and implement #marshal_load(ar) accordingly. That way Marshal can properly handle loops in object graphs etc. Kind regards robert