Ruby 2.4

YAML::Store

class YAML::Store

Parent:PStore

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

公共类方法

initialize( file_name, yaml_opts = {} ) Show source

initialize( file_name, thread_safe = false, yaml_opts = {} )

创建一个新的 YAML :: Store 对象,该对象将存储数据file_name。如果文件不存在,它将被创建。

YAML :: Store 对象始终是可重入的。但是,如果将 thread_safe 设置为 true,那么它将成为线程安全的代价是性能较低。

yaml_opts通过 Object#to_yaml 将商店转换为YAML 时,将使用传入的选项。

调用超类方法 PStore.new

# File lib/yaml/store.rb, line 52 def initialize( *o ) @opt = {} if o.last.is_a? Hash @opt.update(o.pop) end super(*o) end