Search code examples
rubyjsonserializationmarshallingyaml

How can I save an object to a file?


I would like to save an object to a file, and then read it from the file easily. As a simple example, lets say I have the following 3d array:

m = [[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]],
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]]

Is there an easy Ruby API that I can use to achieve this without programming a parser to interpret the data from the file? In the example I give it is easy, but as the objects become more complicated, it gets annoying to make objects persistent.


Solution

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

    -or-

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