From: Joel VanderWerf Date: 2006-05-25T13:01:35+09:00 Subject: Re: Marshaling NArray objects. Alex Polite wrote: > I'm trying to marshal narray objects by first converting them to > ordinary arrays but I can't seem to get it right. I understand that I > have to implement marshal_dump and marshal_load for NArray so I've > tried this: > > class NArray > def marshal_dump > self.to_a > end > > def marshal_load(array) > NArray.to_narray(array) > end > end > > And lots of other stuff. I won't bore you with the error messages, > suffice to say that I can't seem to get it right. One problem is that #marshal_load needs to populate self with the data, rather than just instantiating a new object. In other words, don't bother returning a new NArray from marshal_load--it won't be used. Example: class C attr_accessor :x, :y def marshal_dump [x, y] end def marshal_load(ary) p ary @x, @y = ary # <-- populate self with persisted data end end c = C.new c.x = "foo" c.y = "bar" s = Marshal.dump(c) p Marshal.load(s) __END__ Output: ["foo", "bar"] # Is there a NArray method like Array#replace ? That would probably do the trick, if you called it in marshal_load with the narray as its argument. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407