July 2011
Intermediate to advanced
400 pages
8h 46m
English

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.
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( ...Read now
Unlock full access