On Fri, Jun 29, 2012 at 11:37 PM, Henrique L. <[email protected]> wrote:
> 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

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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to