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
on 2012-06-29 23:37
on 2012-06-30 00:00
You're not opening the file in binary mode for reading (only for writing, earlier). Use ` file = File.new(dir, "rb")`. -- Matma Rex
on 2012-07-02 08:47
On Fri, Jun 29, 2012 at 11:37 PM, Henrique L. <lists@ruby-forum.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.