When I try to save data from a hash to a file, I only get the last line of my hash. What is going wrong?
h= {}
infoArray.zip(href) {|a,b| h[a.to_sym] = b } # i convert two array in hash
File.open("lib/alreadyPass.yml","w") do |file|
file.write h.to_yaml
end
An example of my hash is:
{:"client-1.domaine.net"=>"www.client-1.domaine.net/index.html/xxxxxx"}
{:"client-2.domaine.net"=>"www.client-2.domaine.net/index.html/xxxxxx"}
And the output YAML file I am getting is:
---
:client-1.domaine.net:
- www.client-1.domaine.net/index.html/xxxxxx
The right way would be like this:
require 'yaml'
array_of_hashes = [{:"client-1.domaine.net"=>"www.client-1.domaine.net/index.html/xxxxxx"},{:"client-2.domaine.net"=>"www.client-2.domaine.net/index.html/xxxxxx"}]
File.open("lib/yamlfile.yml","w") do |file|
file.write array_of_hashes.to_yaml
end
This works with a hash of hashes as well...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With