Replacing PStore

PStore is a common Ruby module which serializes objects to a file. PStore is accessed as a Hash by opening a transaction with the file. YAML.rb includes YAML::Store, a drop-in replacement for PStore.

The YAML::Store class simply needs a filename to write to when it is initialized, along with any options you like:

require 'yaml'
y = YAML::Store.new( "/tmp/yaml.store.1", :Indent => 2 )
y.transaction do
  y['names'] = ['Crispin', 'Glover']
  y['hello'] = {'hi' => 'hello', 'yes' => 'YES!!' }
end
Ex. 27: Initializing YAML::Store

Like PStore, the YAML::Store class can store object hierarchies, each identified by a string. The hierarchy is store in a single YAML document as a YAML mapping.

hello:
  hi: hello
  yes: YES!!
names:
  - Crispin
  - Glover
Ex. 28: Dump of /tmp/yaml.store.1