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
公共类方法
源代码
# File lib/yaml/store.rb, line 57 def initialize( *o ) @opt = {} if o.last.is_a? Hash @opt.update(o.pop) end super(*o) end
创建一个新的 YAML::Store
对象,它将数据存储在 file_name
中。如果该文件尚不存在,则会创建它。
YAML::Store
对象始终是可重入的。但是,如果 thread_safe 设置为 true,则它将变为线程安全的,但会牺牲少量性能。
通过 yaml_opts
传入的选项将在通过 Hash#to_yaml()
将存储转换为 YAML
时使用。
调用父类方法
PStore::new