Chapter 15. Marshal

image with no caption

An alternative way of saving and loading data is provided by Ruby’s Marshal library. This has a similar set of methods to YAML to enable you to save and load data to and from disk.

Saving and Loading Data

Compare the following program with yaml_dump2.rb from the previous chapter:

marshal1.rb

f = File.open( 'friends.sav', 'w' ) Marshal.dump( ["fred", "bert", "mary"], f ) f.close File.open( 'morefriends.sav', 'w' ){ |friendsfile| Marshal.dump( ["sally", "agnes", "john" ], friendsfile ) } File.open( 'morefriends.sav' ){ |f| $arr= Marshal.load(f) } myfriends = Marshal.load(File.open( 'friends.sav' )) morefriends = Marshal.load(File.open( ...

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.