Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个带有一些键值的 yml 文件。
age: 24 Name: XYZ
我想要代码将“名称”键的值从 XYZ 更新为 ABC?我该怎么做?
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) }
它将写入 yml 文件。如果文件中不存在指定的键(“名称”),它将写入新的键值,否则将替换现有的键值。