I am trying to use a YAML file, reading from it and writing to it a list of values. On the first run of this script, the yaml file is correctly created, but then on the second it throws a conversion TypeError which I don't know to fix.
db_yml = 'store.yml'
require 'psych'
begin
if File.exist?(db_yml)
yml = Psych.load_file(db_yml)
puts "done load"
yml['reminders']['reminder_a'] = [123,456]
yml['reminders']['reminder_b'] = [457,635,123]
File.write(db_yml, Psych.dump(yml) )
else
#the file does not exist yet, create an empty one.
File.write(db_yml, Psych.dump(
{'reminders' => [
{'reminder_a'=> [nil]},
{'reminder_b'=> [nil]}
]}
)) #Store
end
rescue IOError => msg
# display the system generated error message
puts msg
end
produces the file store.yml on first run:
---
reminders:
- reminder_a:
-
- reminder_b:
-
So far so good. But then on the second run it fails with
done load
yamlstore.rb:23:in `[]=': no implicit conversion of String into Integer (TypeError)
from yamlstore.rb:23:in `<main>'
Could you tell me where I am going wrong?