简体   繁体   English

在 ruby​​ on rails 中更新 yaml 文件的键值

[英]Update value of key of a yaml file in ruby on rails

I have a yml file with some key value.我有一个带有一些键值的 yml 文件。

age: 24
Name: XYZ

I want code to update the value of "Name" key from XYZ to ABC?我想要代码将“名称”键的值从 XYZ 更新为 ABC? How can i do it?我该怎么做?

    require 'yaml'
    data = YAML.load_file "path/to/yml_file.yml"
    data["Name"] = ABC
    File.open("path/to/yml_file.yml", 'w') { |f| YAML.dump(data, f) }

It will write into yml file.它将写入 yml 文件。 If specified key ("Name") is not present in file, it will write new key value othrwise the existing one will be replaced.如果文件中不存在指定的键(“名称”),它将写入新的键值,否则现有的键值将被替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM