From: Jason Creighton Date: 2003-09-18T08:52:04+09:00 Subject: Re: [newbie]saving and reading array of associative array On Wed, 17 Sep 2003 21:22:13 +0900 Joey Gibson wrote: > On 9/17/2003 6:34 AM, gabriele renzi wrote: > > >Marshal::dump > > dump( anObject [, anIO] , limit=--1 ) -> anIO > > > >BTW you could even use PStore, included in the standard lib, it works > >similar to an hash, but gets saved on disk and supports 'transactions' > >for data integrity. > > > > > > You could also use YAML: > > require 'yaml' > one = {"Name" => "Smith", "Surname" => "John"} > two = {"Name" => "Dupont", "Surname" => "Jean"} > > myFriends = [one, two] > > File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)} > myFriends = File.open("myfriends.db") {|file| YAML.load(file)} Yes, I was just about to mention YAML. Also note that you can use YAML::Store as a drop-in PStore replacement. (In fact, this example is the example given in pstore.rb, I just require'd yaml/store, and changed the db = line to YAML::Store) ~/prog/ruby$ cat yaml-store-example.rb require 'yaml/store' db = YAML::Store.new("/tmp/foo") db.transaction do p db.roots ary = db["root"] = [1,2,3,4] ary[0] = [1,1.5] end db.transaction do p db["root"] end ~/prog/ruby$ ruby yaml-store-example.rb [] [[1, 1.5], 2, 3, 4] ~/prog/ruby$ cat /tmp/foo --- root: - - 1 - 1.5 - 2 - 3 - 4 ~/prog/ruby$ Jason Creighton