将 Ruby 哈希转换为 YAML [英] Convert Ruby Hash into YAML

查看:19
本文介绍了将 Ruby 哈希转换为 YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将下面提供的哈希值转换为可读的 YAML.看起来我可以提供 YAML::load 一个字符串,但我想我需要先将它转换成这样的:

hostname1.test.com:公众:51私人:10{"hostname1.test.com"=>{公共"=>51",私人"=>10"},hostname2.test.com"=>{公共"=>192",私人"=>12"}}

我不确定如何有效地将转换为该字符串.

我查看了 HASH 文档,找不到任何关于 to_yaml 的内容.我通过搜索 to_yaml 找到了它,当你 require yaml 时它变得可用.我也尝试使用 Enumerable 方法 collect 但是当我需要遍历值(另一个哈希)时感到困惑.

我正在尝试使用在 Ruby 中将哈希转换为字符串"作为参考.然后我的想法是将其输入 YAML::load 并生成我想要的 YAML.

解决方案

我会这样做:

需要'yaml'HASH_OF_HASHES = {hostname1.test.com"=>{公共"=>51",私人"=>10"},hostname2.test.com"=>{公共"=>192",私人"=>12"}}ARRAY_OF_HASHES = [{"hostname1.test.com"=>{公共"=>51",私人"=>10"}},{"hostname2.test.com"=>{公共"=>192",私人"=>12"}}]将 HASH_OF_HASHES.to_yaml看跌期权将 ARRAY_OF_HASHES.to_yaml

哪些输出:

---主机名1.test.com:公开:'51'私人:'10'主机名2.test.com:公开:'192'私人:'12'---- 主机名1.test.com:公开:'51'私人:'10'- 主机名2.test.com:公开:'192'私人:'12'

<小时><块引用>

Object 类有一个 to_yaml 方法.我使用了它,它正确地生成了 YAML 文件.

不,它没有.

这是来自新打开的 IRB 会话:

Object.instance_methods.grep(/to_yaml/)=>[]需要'yaml'=>真的Object.instance_methods.grep(/to_yaml/)=>[:psych_to_yaml,:to_yaml,:to_yaml_properties]

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.

解决方案

Here's how I'd do it:

require 'yaml'

HASH_OF_HASHES = {
  "hostname1.test.com"=> {"public"=>"51", "private"=>"10"},
  "hostname2.test.com"=> {"public"=>"192", "private"=>"12"}
}

ARRAY_OF_HASHES = [
  {"hostname1.test.com"=> {"public"=>"51", "private"=>"10"}},
  {"hostname2.test.com"=> {"public"=>"192", "private"=>"12"}}
]

puts HASH_OF_HASHES.to_yaml
puts
puts ARRAY_OF_HASHES.to_yaml

Which outputs:

---
hostname1.test.com:
  public: '51'
  private: '10'
hostname2.test.com:
  public: '192'
  private: '12'

---
- hostname1.test.com:
    public: '51'
    private: '10'
- hostname2.test.com:
    public: '192'
    private: '12'


The Object class has a to_yaml method. I used that and it generated the YAML file correctly.

No, it doesn't.

This is from a freshly opened IRB session:

Object.instance_methods.grep(/to_yaml/)
=> []
require 'yaml'
=> true
Object.instance_methods.grep(/to_yaml/)
=> [:psych_to_yaml, :to_yaml, :to_yaml_properties]

这篇关于将 Ruby 哈希转换为 YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆