为 YAML 文件提供多个键,同时仅对键使用一个值

Giving a YAML file more than one key, while only using one value for the keys

我有一个问题,我需要制作一个包含以下键的 yaml 文件:

ETA SOL VETS EMC

从这些键中,我需要一个值,该值将是一个电子邮件地址,所有四个键都是相同的电子邮件地址,是否可以制作一个包含多个键且只有一个值的 yaml 文件..?

例如:

agencies:
         - ETA
         - SOL
         - VETS
         - EMC
            advocate_email: "example@example.com" #<= Give these four the same value
         - some
         - other
         - ones
             advocate_email: "example1@example1.com" #<= Give three another value.. So one and so forth

我不知道我是否理解正确,但我想你想要类似的东西

emails:
  ETA: email1@example.com
  SOL: email2@example.com
  VETS: email3@example.com

** 更新 ** 我想你有多个电子邮件,每个组都有一个更长的列表。

group1:
  email: me@email.com
  list:
    - ETA
    - SOL
    - VETS
group2:
  email: me2@email.com
  list:
    - ONE
    - TWO
    - THREE

** 结束更新**

如果我理解正确并且你想要同一封电子邮件:

email: &email me@email.com

emails:
  ETA: *email
  SOL: *email
  VETS: *email

输出:

pry(main)> YAML.load(File.read('foo.yml'))
=> {"email"=>"me@email.com", "emails"=>{"ETA"=>"me@email.com", "SOL"=>"me@email.com", "VETS"=>"me@email.com"}}

我不确定这是解决您问题的最佳方法,但您可以使用任何东西作为 YAML 映射中的键,包括序列(数组)。它看起来像这样:

agencies:
  ? - ETA
    - SOL
    - VETS
    - EMC
  : advocate_email: example@example.com
  ? - some
    - other
    - ones
  : advocate_email: example1@example1.com

每个?表示一个键,随后的:表示一个值。演示:

require "pp"
require "yaml"

yaml = <<YML
agencies:
  ? - ETA
    - SOL
    - VETS
    - EMC
  : advocate_email: example@example.com
  ? - some
    - other
    - ones
  : advocate_email: example1@example1.com
YML

pp YAML.load(yaml)
# => {"agencies"=>
#      {["ETA", "SOL", "VETS", "EMC"]=>{"advocate_email"=>"example@example.com"},
#       ["some", "other", "ones"]=>{"advocate_email"=>"example1@example1.com"}}}