首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

YAML::Store

Parent:PStore

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

代码语言:javascript
复制
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” 的内容将是:

代码语言:javascript
复制
---
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

代码语言:javascript
复制
# 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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com