1. Computing

Serialization in Ruby: Marshal

By

2 of 3

Caveats

Marshal is not the perfect solution in all situations. It has a number of drawbacks and limitations.

Marshal stores objects in a binary format. This format is not transparent at all, only the Marshal code knows how to read and write this format. This means only Marshal should be reading and writing objects serialized with Marshal. The format is also subject to change, so even if you send a marshaled object to another program, it might not be able to read it.

This also means marshaled objects can't be edited by hand. Many people use things like YAML or XML for configuration files, simply unserializing them into their Ruby programs. This is not possible with Marshal, at least not without a separate script to unmarshal, allow editing, and re-marshal.

Not all objects can be marshaled. If the object to be marshaled contains a block, a binding (a reference to a particular part in a running Ruby program), or an IO object, it cannot be marshaled. Also, if an object contains singleton objects (objects whose type has been modified at runtime to add extra methods), it cannot be marshaled. You can get around this though, see the next page.

  1. About.com
  2. Computing
  3. Ruby
  4. Advanced Ruby
  5. Serialization
  6. Caveats

©2014 About.com. All rights reserved.