如何将值保存到 YAML 文件中? [英] How do you save values into a YAML file?

查看:95
本文介绍了如何将值保存到 YAML 文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 persist.yml 文件中.我有以下键值对...

Inside my persist.yml file. I have the following key-value pair...

session = 0

如何更新 YAML 文件,以便:

How do I update the YAML file such that:

session = 2

推荐答案

使用 ruby​​-1.9.3(方法可能不适用于旧版本).

Using ruby-1.9.3 (Approach may not work in older versions).

我假设文件看起来像这样(相应地调整代码):

I'm assuming the file looks like this (adjust code accordingly):

---
content:
    session: 0

被称为/tmp/test.yml

and is called /tmp/test.yml

那么代码就是:

require 'yaml' # Built in, no gem required
d = YAML::load_file('/tmp/test.yml') #Load
d['content']['session'] = 2 #Modify
File.open('/tmp/test.yml', 'w') {|f| f.write d.to_yaml } #Store

这篇关于如何将值保存到 YAML 文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆