From: Erik Veenstra Date: 2007-06-27T22:46:02+09:00 Subject: Re: Fun about inspect > What do you think? In one specific situation, I use a journal to resemble a small DB. The reason to use a journal is the requirement to be able to go back to _any_ moment in time. When adding, changing or deleting an object to the hash, the corresponding line is written to the journal. For speed, the hash and the length and MD5 checksum of the journal are dumped (using Marshal) to a file at the end of the program. On startup, this file is loaded and the length and MD5 checksum of the journal are checked. If the length and MD5 checksum don't match or Marshal.load fails for whatever reason, the database is rebuild using the journal (which might take a while... :}). This gives us the possibility to go back in time. And it's a nice way to ensure that all data gets effectively written when the application ends. I even added commits to the journal. When loading the journal, everything after the last commit is disregarded. Loading the journal is done with the code below (simplified). As long as the objects inspect to Ruby syntax, this approach works fine. gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- # journal [{:action=>:added, :timestamp=>"20070627140031"}, {:id=>"aaa", :something=>17}] [{:action=>:added, :timestamp=>"20070627140031"}, {:id=>"bbb", :something=>18}] [{:action=>:changed, :timestamp=>"20070627140031"}, {:id=>"bbb", :something=>19}] [{:action=>:deleted, :timestamp=>"20070627140033"}, {:id=>"bbb"}] ---------------------------------------------------------------- # read_journal.rb items = {} File.open("journal") do |f| f.readlines.each do |line| raise "line doesn't end with \\n, probably caused by a previous IO error [#{line}]" unless line[-1..-1] == "\n" action, item = Thread.new{$SAFE=4; eval(line, Module.new.module_eval{binding})}.value case action[:action] when :added then items[item[:id]] = item when :changed then items[item[:id]] = item when :deleted then items.delete(item[:id]) else raise "unknown action [#{action.inspect}]" end end end p items ----------------------------------------------------------------