End of file error when trying to Marshal.load

  • Thread starter Vahagn Hayrapetyan
  • Start date
V

Vahagn Hayrapetyan

Hi,-

I am trying out the marshaling functionality of Ruby with this
documentation as a guide:

http://www.ruby-doc.org/core/classes/Marshal.html

Code goes like this:

*****************************

class Klass
def initialize(str)
@str = str
end
def sayHello
@str
end
end

def marshalKlass
o = Klass.new("hello\n")
print o.sayHello
Marshal.dump(o, File.open("output.txt", "w"))
data = File.open("output.txt") # all fine up until here
obj = Marshal.load(data) # EOFError
print obj.sayHello
end

if __FILE__ == $PROGRAM_NAME
marshalKlass
end

*****************************

The line:
obj = Marshal.load(data)
gives the following error:

Klass.rb:19:in `load': end of file reached (EOFError)
from Klass.rb:19:in `marshalKlass'
from Klass.rb:32

The line:
data = File.open("output.txt") places the cursor at the start of the
file. What's wrong?

Thanks!

/ Vahagn
 
T

Tim Hunter

Vahagn said:
The line:
obj = Marshal.load(data)
gives the following error:

Klass.rb:19:in `load': end of file reached (EOFError)
from Klass.rb:19:in `marshalKlass'
from Klass.rb:32

The line:
data = File.open("output.txt") places the cursor at the start of the
file. What's wrong?

I'm guessing you're on Windows. You need to open the file in binary
mode. That is, with "wb" and "rb".
 
V

Vahagn Hayrapetyan

Tim, I'm on Mac OS X (Leopard). Now I have:

Marshal.dump(o, File.open("output.txt", "wb"))
data = File.open("output.txt", "rb")
obj = Marshal.load(data)

But I get the exact same error.

/ Vahagn
 
V

Vahagn Hayrapetyan

@Robert:

Ah! "While not closing a read only file usually does not have dramatic
consequences, not closing a file opened for writing likely has dramatic
consequences."

The working method:

def marshalKlass
o = Klass.new("hello\n")
print o.sayHello
dumped = File.open("output.txt", "w")
Marshal.dump(o, dumped)
dumped.close
data = File.open("output.txt", "r")
obj = Marshal.load(data)
data.close
print obj.sayHello
end


Thanks,
/ Vahagn
 
R

Robert Klemme

@Robert:

Ah! "While not closing a read only file usually does not have dramatic
consequences, not closing a file opened for writing likely has dramatic
consequences."

The working method:

def marshalKlass
o = Klass.new("hello\n")
print o.sayHello
dumped = File.open("output.txt", "w")
Marshal.dump(o, dumped)
dumped.close
data = File.open("output.txt", "r")
obj = Marshal.load(data)
data.close
print obj.sayHello
end

This is better but my point in the blog article was a different one:
use the block form of File.open because it is more robust.

Cheers

robert
 
V

Vahagn Hayrapetyan

@Robert: Sure!

def marshal_class
o = Klass.new("hello")
print o.sayHello, " from original object\n"
File.open("data.txt", "w") do |file|
Marshal.dump(o, file)
end
File.open("data.txt", "r") do |file|
@obj = Marshal.load(file)
end
print @obj.sayHello + " from restored object\n"
end

That's a good article, btw.
Cheers,
Vahagn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top