ruby 如何将 YAML 解析为哈希/对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16324567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:55:23  来源:igfitidea点击:

How do I parse YAML into a hash/object?

rubyyaml

提问by Kevin Whitaker

I have a YAML file with a few entries that look like this:

我有一个 YAML 文件,其中包含一些如下所示的条目:

001:
  :title: Some title
  :description: Some body text maybe
002:
  :title: Some title
  :description: Some body text maybe

I'm using the following Ruby method to parse that YAML file into a set of objects I can iterate over:

我正在使用以下 Ruby 方法将该 YAML 文件解析为一组我可以迭代的对象:

def parse_yaml(file)
  YAML::load(File.open(File.join(settings.yaml_folder, file)))
end

def use_yaml
  @items = parse_yaml('items.yml')
  @items.each do |item|
    x = item[1][:title]
    etc...
  end
end

Now, that method works, but I find it queer that I need to use item[1][:title]to access the attributes of the object I'm iterating over. How can I build my YAML file or my parsing code to allow me to use the more standard item[:title]?

现在,该方法有效,但我发现它很奇怪,我需要使用它item[1][:title]来访问我正在迭代的对象的属性。我如何构建我的 YAML 文件或我的解析代码以允许我使用更标准的item[:title]

采纳答案by Ivan Black

It's a Hash. The parse_yamloutput is:

这是一个哈希。该parse_yaml输出是:

{ 1=>
      { :title=>"Some title",
        :description=>"Some body text maybe"},
  2=> { :title=>"Some title",
        :description=>"Some body text maybe" }
}

You may to use the each_valuemethod like this:

您可以使用这样的each_value方法:

#...
@items = parse_yaml('items.yml')
@items.each_value do |item|
    x = item[:title]
    # ... etc
end

Recomend: YAML for Ruby

推荐:Ruby 的 YAML

回答by Robert Mitchell

The underlying issue is that your YAML file is storing your data as a hash, and trying to access it like an array.

潜在的问题是您的 YAML 文件将您的数据存储为散列,并尝试像数组一样访问它。

To convert your data into array format:

要将数据转换为数组格式:

---
- :title: Some title
  :description: Some body text maybe
- :title: Some title
  :description: Some body text maybe

Also interesting to note, the reason you had to use item[1][:title]to reference your items is that the keys 001and 002are converted to integers by YAML.load.

同样有趣的是,您必须使用item[1][:title]来引用您的项目的原因是键001002被 YAML.load 转换为整数。

You can confirm this in irb:

您可以在 irb 中确认这一点:

irb(main):015:0> YAML.load(File.open("./test.yml"))
=> {1=>{:title=>"Some title", :description=>"Some body text maybe"}, 2=>{:title=>"Some title", :description=>"Some body text maybe"}}

回答by Frederick Cheung

Your YAML is the serialisation of a hash so you could do:

您的 YAML 是哈希的序列化,因此您可以执行以下操作:

@items.each do |key, item|
  #do something with item[:title]
end

Or change your YAML to look like:

或者将您的 YAML 更改为如下所示:

- :title: blah
  :description: description
- :title: second title
  :description: second description

Which will result in YAML.loadreturning an array.

这将导致YAML.load返回一个数组。