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:, “w”) do |f|
f.write Marshal::dump(@map)

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

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:, “w”) do |f|
f.write Marshal::dump(@map)

You want binary mode. Plus, you can directly hand over the stream:, ‘wb’) do |f|
Marshal::dump(@map, f)




It is working. Loading without error.