Omitting Variables on Saving

As with YAML serialization, it is possible to limit the variables that are saved when serializing using Marshal. In YAML, you did this by writing a method called to_yaml_properties. With Marshal, you need to write a method named marshal_dump. In the code of this method you should create an array containing the actual variables to be saved (in YAML, you created an array of strings containing the variable names).

This is an example:

def marshal_dump
    [@variable_a, @variable_b]
end

Another difference is that, with YAML, you were able simply to load the data in order to re-create an object. With Marshal, you need to add a special method called marshal_load to which any loaded data is passed as an argument. This will be invoked ...

Get The Book of Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.