把标准数据结构转换成扁平数据形式,可被保存到磁盘或通过网络传输,供以后重新构造数据结构
class Person
attr_accessor :name
end
fred = Person.new
fred.name = 'Fred Bloggs'
laura = Person.new
laura.name = 'Laura Smith'
require 'pstore'
store = PStore.new('storagefile')
store.transaction do
store[:people] ||= Array.new
store[:people] << fred
store[:people]<<laura
end
#以后就可以从PStore数据库中检索数据
require 'pstore'
store = PStore.new('storagefile')
people = []
store.transaction do
people = store[:people]
end
people.each do |p|
puts p.name
end