From: Robert Klemme Date: 2012-02-09T22:15:01+09:00 Subject: Re: Problem migrating to Ruby 1.9.2 On Thu, Feb 9, 2012 at 2:23 AM, Patrick Bayford wrote: > Robert Klemme wrote in post #1044064: >> On Sat, Feb 4, 2012 at 4:24 AM, Patrick Bayford >> wrote: >>>> >>>> Did you consider using Marshal? >>>> >>>>> I'd prefer using a proper database but can't get any of those to behave >>>>> properly either!!! >>>> >>>> What does that mean? >> >>> I did try using Marshal, however, while the examples show how to use >>> dump & load for in memory use, there was not enough information for me >>> to get it to write to a file! I don't have enough Ruby experience yet to >>> work out how to do this sort of stuff - I'm a Pascal/Delphi man by >>> choice, and serializing objects is nearly all down to the coder. >> >> The docs at >> http://www.ruby-doc.org/core-1.9.3/Marshal.html#method-c-dump >> mention that you can pass an IO object. >> >> file_name = "foo.bin" >> >> # store >> File.open(file_name, 'wb') {|io| Marshal.dump(obj, io)} >> >> # load >> obj = File.open(file_name, 'rb') {|io| Marshal.load(io)} >> >> You should take care that "obj" is an object containing everything you >> want to serialize. >> >>> As to the database comment, I have so far been unable to get mySQL >>> working with Ruby 1.9, and don't (yet) know enough SQLite to make it's >>> use practical. I have SQL Server too, but that has even poorer support >>> documentation, as far as Ruby is concerned. Most of my attempts have >>> failed at the connect stage. >> >> I'd try Marshal first.  Much simpler to use and quite fast. > Thanks for the assist Robert - marshal does indeed work - the key for me > was the IO reference, which is not well explained in the reference > material! > However, I have also discovered PStore, which is more convenient for me, > as it will keep several objects in the same file. No, the main advantage of PStore over plain vanilla Marshal is transactional behavior - not the fact that you can store Hashes. A Hash can be easily stored with Marshal as well. As I said: "You should take care that "obj" is an object containing everything you want to serialize." The only difference here is that with PStore you can read and write individual key value pairs while with Marshal you need to read and write the complete Hash. It is just an interface thing though and PStore reads and writes the complete Hash internally as well: irb(main):010:0> f='x.pstore' => "x.pstore" irb(main):013:0> ps = PStore.new f => #> irb(main):014:0> ps.transaction {ps['a']=Time.now} => 2012-02-09 14:12:17 +0100 irb(main):015:0> File.open(f,'rb') {|io| Marshal.load(io)} => {"a"=>2012-02-09 14:12:17 +0100} irb(main):016:0> ps.transaction {ps['b']=Time.now} => 2012-02-09 14:12:36 +0100 irb(main):017:0> File.open(f,'rb') {|io|Marshal.load(io)} => {"a"=>2012-02-09 14:12:17 +0100, "b"=>2012-02-09 14:12:36 +0100} Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/