From: ara.t.howard@... Date: 2006-05-25T14:55:34+09:00 Subject: Re: Marshaling NArray objects. On Thu, 25 May 2006, Alex Polite wrote: > On 5/25/06, Joel VanderWerf wrote: >> def marshal_load(ary) >> p ary >> @x, @y = ary # <-- populate self with persisted data >> end > >> >> 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. >> > > There doesn't seem to be anything like replace in narray. Maybe I > should resort to using a wrapper class around my narrays? > > class NArrayWrapper > def initialize(narray) > @narray = narray > end > > def marshal_dump > @narray.to_a > end > > def marshal_load(array) > @narray = NArray.to_narray(array) > end > end > > But then I'd have to write a whole bunch of accessor methods and stuff. > I bet there's some C/ruby/narray wizardry that will replicate replace. are you sure need it? you can write/read narrays from file quite easily: harp:~ > cat a.rb require 'narray' na = NArray.int 2, 3 na[0, true] = 42 p na open('dat', 'w'){|f| f.write na.to_s} buf = IO.read('dat') na = NArray.to_na buf, NArray::INT, 2, 3 p na harp:~ > ruby a.rb NArray.int(2,3): [ [ 42, 0 ], [ 42, 0 ], [ 42, 0 ] ] NArray.int(2,3): [ [ 42, 0 ], [ 42, 0 ], [ 42, 0 ] ] so, assuming that you do, it's easy enough: harp:~ > cat a.rb require 'narray' class NArray def _dump *ignored Marshal.dump :typecode => typecode, :shape => shape, :data => to_s end def self._load buf h = Marshal.load buf typecode = h[:typecode] shape = h[:shape] data = h[:data] to_na data, typecode, *shape end end na = NArray.int 2, 3 na[0, true] = 42 dumped = Marshal.dump na na = Marshal.load dumped p na harp:~ > ruby a.rb NArray.int(2,3): [ [ 42, 0 ], [ 42, 0 ], [ 42, 0 ] ] regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama