Ruby Programming/Reference/Objects/Marshal

From Wikibooks, open books for an open world
< Ruby Programming‎ | Reference‎ | Objects
Jump to: navigation, search


The Marshal class is used for serializing and de-serializing objects to disk:


serialized = Marshal.dump(['an', 'array', 'of', 'strings'])
unserialized = Marshal.restore(serialized)

With 1.9, each dump also includes an encoding, so you *must* use Marshal's stream read feature if you wish to read it from an IO object, like a file.

a ="serialized_data", "w")
a.write Marshal.dump(33)
b ="serialized_data", "r")
unserialized = Marshal.restore(b)