Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 647
Sonal S. Avatar asked Dec 19 '12 09:12

Sonal S.


1 Answers

    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.

like image 75
Sonal S. Avatar answered Oct 04 '22 11:10

Sonal S.