Marshal.load error

I’ve been trying hard to find a smart way of serializing objects using
the Marshal module, but there is a few issues which I need some help to
fix.

I’m getting statuses from the Twitter stream and saving them all in
separated files (that can contain one or more tweet objects) using
Marshal.dump (the file is opened using
File.new("./src/tweets/#{filename}.adl", “a+b”), where filename is a
string generated from the tweet id), but when I try to unserialize the
tweets all at once I get the following error:

test.rb:8: in 'restore: incompatible marshal file format (can’t be read)
(TypeError)
format version 4.8 required; 128.0 given
[…]

Here’s the code that tries to unserialize the tweets from the file:

result = Dir.glob("./tweets/*.adl")
result.each do |dir|
file = File.new(dir, “r”)
tweets = Array.new
until file.eof?
tweets << Marshal.restore(file)
end
end

You’re not opening the file in binary mode for reading (only for
writing, earlier). Use file = File.new(dir, "rb").

– Matma R.

On Fri, Jun 29, 2012 at 11:37 PM, Henrique L. [email protected]
wrote:

file = File.new(dir, “r”)
tweets = Array.new
until file.eof?
tweets << Marshal.restore(file)
end
end

Apart from the binmode issue that Bartosz mentioned you are not
closing files, you use a useless #eof? and you have the scope of
“tweets” wrong. I’d do

tweets = Dir[“tweets/*.adl”].map do |dir|
File.open(dir, “rb”) {|file| Marshal.restore(file)}
end

Kind regards

robert