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

时间:2023-01-15 09:13:44

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文件或我的解析代码以允许我使用更标准的项目[:title]?

3 个解决方案

#1


8  

It's a Hash. The parse_yaml output 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_value method like this:

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

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

Recomend: YAML for Ruby

Recomend:YAML for Ruby

#2


3  

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 001 and 002 are converted to integers by YAML.load.

另外有趣的是,你必须使用item [1] [:title]引用你的项目的原因是键001和002由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"}}

#3


1  

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.load returning an array.

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

#1


8  

It's a Hash. The parse_yaml output 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_value method like this:

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

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

Recomend: YAML for Ruby

Recomend:YAML for Ruby

#2


3  

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 001 and 002 are converted to integers by YAML.load.

另外有趣的是,你必须使用item [1] [:title]引用你的项目的原因是键001和002由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"}}

#3


1  

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.load returning an array.

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