输出带有格式的 yaml 文件键的所有值

Output all values of a yaml file key with formatting

我有一个 yaml 文件,其中有几个键有多个值:

inbox:
  mary:
          - '-MD-C-LAUREL-ADMIN'
          - '-MD-E-BALTIMORE-ADMIN'
          - '-MD-R1-CAMBRIDGE-ADMIN'
          - '-MD-R2-BELAIR-ADMIN'
          - '-MD-R4-LAUREL-ADMIN'
          - '-MD-R5-HAGERSTWN-ADMIN'
  mike:
          - '-VA-E-Richmond-Admin'
          - '-VA-Richmond-Admin'
          - '-VA-Manassas-Admin'
          - '-VA-Norfolk-Admin'
          - '-VA-Roanoke-Admin'
          - '-VA-Verona-Admin'
  max: '-ATLANTA-RO-ADMIN'

YAML 文件'test.yml'

POC = YAML.load_file('test.yml')    

def get_info(name)
  POC['inbox'][name.downcase].nil? ? "** #{name} IS NOT POC **" : POC['inbox'][name.downcase]
end

我想做的是以某种格式输出所有这些键,例如当这是 运行:

irb(main):003:0> require 'yaml'
=> true
irb(main):004:0> POC = YAML.load_file('test.yml')
=> {"inbox"=>{"mary"=>["-MD-C-LAUREL-ADMIN", "-MD-E-BALTIMORE-ADMIN", "-MD-R1-CAMBRIDG
E-ADMIN", "-MD-R2-BELAIR-ADMIN", "-MD-R4-LAUREL-ADMIN", "-MD-R5-HAGERSTWN-ADMIN"], "mi
ke"=>["-VA-E-Richmond-Admin", "-VA-Richmond-Admin", "-VA-Manassas-Admin", "-VA-Norfolk
-Admin", "-VA-Roanoke-Admin", "-VA-Verona-Admin"]}}
irb(main):005:0>     def get_info(name)
< "** #{name} IS NOT POC **" : POC['inbox'][name.downcase]
irb(main):007:1>     end
=> :get_info
irb(main):008:0> get_info('mary')
=> ["-MD-C-LAUREL-ADMIN", "-MD-E-BALTIMORE-ADMIN", "-MD-R1-CAMBRIDGE-ADMIN", "-MD-R2-B
ELAIR-ADMIN", "-MD-R4-LAUREL-ADMIN", "-MD-R5-HAGERSTWN-ADMIN"]
irb(main):009:0>

预期的输出是:

Possibly one of the following inboxes: 
1. -MD-C-LAUREL-ADMIN
2. -MD-E-BALTIMORE-ADMIN
3. -MD-R1-CAMBRIDGE-ADMIN
4. -MD-R2-BELAIR-ADMIN
5. -MD-R4-LAUREL-ADMIN
6. -MD-R5-HAGERSTWN-ADMIN

如果有多个值,我该如何输出数组中的信息与数字匹配?

您的问题与YAML 完全无关。这都是关于如何打印出带有索引的数组。

%w(foo bar baz).each.with_index(1) do |str, i|
  puts "#{i}. #{str}"
end

puts %w(foo bar baz).each.with_index(1).map{|str, i| "#{i}. #{str}"}