简体   繁体   中英

Update value of key of a yaml file in ruby on rails

I have a yml file with some key value.

age: 24
Name: XYZ

I want code to update the value of "Name" key from XYZ to 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. If specified key ("Name") is not present in file, it will write new key value othrwise the existing one will be replaced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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