Joel VanderWerf
5/21/2008 8:16:00 PM
sayoyo Sayoyo wrote:
> Hi,
>
> I have a problem with the marshal_dump and marshal_load...
> I built two classes XXX, YYY and implemented both methods like this:
>
> class YYY
> @data = "string"
> @version = 1
>
> def marshal_dump()
> return [@version,@data]
> end
>
> def marshal_load(var)
> @version = var[0]
> case @version
> when 1
> @data = var[1]
> else
> #do something else
> end
> end
> end
>
> class XXX
> @data = Array of YYY
> @version = 1
>
> def marshal_dump()
> return [@version,@data]
> end
>
> def marshal_load(var)
> @version = var[0]
> case @version
> when 1
> @data = var[1]
> else
> #do something else
> end
> end
> end
>
> The serialization works correctly when the number of objects YYY is
> small, but when it got more than 5000, the serialization doesn't work.
> (the serializaed file has more and less 1 meg)
>
>
> When I try to not to use marshal_dump and marshal_load. the
> serialization works again????
>
> Can someone tell me, where is the difference between customized
> serialization and no-customized one???
>
> Thanks you very much
>
> Sayoyo
What version of ruby? What platform?
Same problem when you serialize to memory instead of file?
What exactly is the observed problem? Crash, exception, data error?
The following version of your code seems to work:
class YYY
attr_reader :data, :version
def initialize
@data = "string"
@version = 1
end
def marshal_dump()
return [@version,@data]
end
def marshal_load(var)
@version = var[0]
case @version
when 1
@data = var[1]
else
#do something else
end
end
end
class XXX
attr_reader :data, :version
def initialize
@data = (0..1000).map { YYY.new }
@version = 1
end
def marshal_dump()
return [@version,@data]
end
def marshal_load(var)
@version = var[0]
case @version
when 1
@data = var[1]
else
#do something else
end
end
end
xxx = XXX.new
x2 = Marshal.load(Marshal.dump(xxx))
p x2.data[777]
File.open('/tmp/foo', "wb") do |f|
Marshal.dump(xxx, f)
end
x3 = File.open('/tmp/foo', "rb") do |f|
Marshal.load(f)
end
p x3.data[888]
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407