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

  • 本文关键字:密钥 一个 文件 YAML ruby key yaml
  • 更新时间 :
  • 英文 :


我有一个问题,我需要创建一个包含以下键的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

** 更新 **我认为您有多封电子邮件,每个组都有更长的列表。

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"}}}

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

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

相关内容

最新更新