Reputation: 61
Here is the YAML file:
test:
title: ABC Company
compId: '5161'
accounts:
- id: 1
title: ABC Company
MediaTypes:
- title: AAA Company
id: 66
isSelected: true
I'm trying to write Ruby code to replace id: 1 (next level to accounts) to id: 2.
Here is my ruby code:
data = YAML::load(File.open(File.expand_path("../../../data/test.yml", __FILE__)))
data["test"]["accounts"]["id"] = 2
File.open((File.expand_path("../../../data/"test.yml", __FILE__)), 'w') {|f| f.write data.to_yaml }
When I run the script, the result is:
test:
title: ABC Company
compId: '5161'
accounts: 2
Could someone please tell me what I did wrong? Thanks!
Upvotes: 0
Views: 1381
Reputation: 35803
Try this instead:
data["test"]["accounts"][0]["id"] = 2
Remember that accounts is an array of hashes, not a hash.
Upvotes: 1