From: Joel VanderWerf Date: 2006-05-25T17:26:15+09:00 Subject: Re: Marshaling NArray objects. Alex Polite wrote: > On 5/25/06, Joel VanderWerf wrote: > >> This seems to work as a replace method: >> >> a[] = b[] >> > > =============== > 1 require 'narray' > 2 class NArray > 3 def marshal_dump > 4 self.to_a > 5 end > 6 > 7 def marshal_load(array) > 8 newnarray = NArray.to_narray(array) > 9 self[] = newnarray[] > 10 end > 11 end > 12 > 13 na = NArray.float(10) > 14 > 15 Marshal.load(Marshal.dump(na)) > =============== > > Results in > > marshaling_narrays.rb:9:in `[]=': wrong argument type NArray (expected > Data) (TypeError) > from marshaling_narrays.rb:9:in `marshal_load' > from marshaling_narrays.rb:15 > > That's a bug in NArray. It should define an allocator function by calling rb_define_alloc_func(), otherwise the T_OBJECT allocator is used instead of an allocator that returns a T_DATA. In this case, #marshal_load is being called on an object that is already corrupt, before we even try to replace its contents: it is marked as T_OBJECT, and so NArray's methods will fail on it, expecting T_DATA. In any case, with the bug fixed or not, this won't work as written. The typecode and shape would have to be added to the persisted data, as Ara's solution points out. But more that that, there would be no way for NArray to correctly allocate storage without knowing the typecode, so the load method has to be a class method (_load), not an instance method (marshal_load). -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407