Marshal load: undefined class/module Field

I’m saving to file Map class, which contains @width, @height, both
integers, and 2D array @fields of Field class. Field class contains @x,
@y, @terrain, @river, @forest, all are integers.

I’m saving using code:
File.open(filename, “w”) do |f|
f.write Marshal::dump(@map)
end

Next, in the same app, I’m trying to load map from file using:
File.open(filename, “rb”) do |f|
@map = Marshal::load(f)
end

I have got an ArgumentError in ‘load’: undefined class/module Field
which is raised when definition of class is not known.

But in my example I’m using require ‘map.rb’ and require ‘field.rb’ so
definitions of Map and Field are known.

I’ve tried to create new Field instance in a line before loading from
file, and it works with no error. So the Field class must be known.

What is a problem with this example? Why Field class is still undefined?
Thanks for help.

On Wed, Jun 20, 2012 at 12:55 PM, Mateusz Winiarski
[email protected] wrote:

I’m saving using code:
File.open(filename, “w”) do |f|
f.write Marshal::dump(@map)
end

You want binary mode. Plus, you can directly hand over the stream:

File.open(filename, ‘wb’) do |f|
Marshal::dump(@map, f)
end

Cheers

robert

Thanks.

It is working. Loading without error.