I need to convert a hash like the one provided below into readable YAML. It looks like I can feed YAML::load a string, but I think I need to convert it first into something like this:
hostname1.test.com:
public: 51
private: 10
{"hostname1.test.com"=>
{"public"=>"51", "private"=>"10"},
"hostname2.test.com"=>
{"public"=>"192", "private"=>"12"}
}
I'm not sure exactly how to do that conversion into that string effectively though.
I looked through the HASH documentation and couldn't find anything for to_yaml. I found it by searching for to_yaml which becomes available when you require yaml. I also tried to use the Enumerable method collect but got confused when I needed to iterate through the value (another hash).
I'm trying to use "Converting hash to string in Ruby" as a reference. My thought was then to feed that into YAML::load and that would generate the YAML I wanted.
to_yamlis not part of Hash or Object or any class by default. You HAVE torequire 'yaml'someplace in the script, or in something you require, for YAML to extend Object, Hash, and other methods. See the edit to my answer. - the Tin Man