class YAML::Store

YAML::Store 提供了与 PStore 相同的功能,不同之处在于它使用 YAML 来转储对象,而不是 Marshal

示例

require 'yaml/store'

Person = Struct.new :first_name, :last_name

people = [Person.new("Bob", "Smith"), Person.new("Mary", "Johnson")]

store = YAML::Store.new "test.store"

store.transaction do
  store["people"] = people
  store["greeting"] = { "hello" => "world" }
end

运行上述代码后,“test.store”文件的内容将是:

---
people:
- !ruby/struct:Person
  first_name: Bob
  last_name: Smith
- !ruby/struct:Person
  first_name: Mary
  last_name: Johnson
greeting:
  hello: world