我需要将下面提供的散列转换为可读的YAML.看起来我可以输入YAML::load
个字符串,但我想我需要先把它转换成这样的东西:
hostname1.test.com:
public: 51
private: 10
{"hostname1.test.com"=>
{"public"=>"51", "private"=>"10"},
"hostname2.test.com"=>
{"public"=>"192", "private"=>"12"}
}
但我不确定如何有效地将其转换为字符串.
我查了一下散列文件,没有找到to_yaml
美元的任何东西.我通过搜索to_yaml
找到了它,当你搜索require yaml
时,它就变得可用了.我还try 使用可枚举方法collect
,但当我需要迭代该值(另一个哈希)时,我感到困惑.
我想用"Converting hash to string in Ruby"作为参考.我当时的 idea 是将其输入YAML::load
,然后生成我想要的YAML.