有条件地检查yaml文件以显示正确的内容

Raj*_*Das 6 yaml

我怎样才能检if / else入yaml文件.

喜欢:

 if %{attribute}
    attributes:
       shipping_comment: Shipping comment / Instructions
 else
    attributes:
       shipping_date: Date
Run Code Online (Sandbox Code Playgroud)

Pau*_*nti 11

YAML是一种数据序列化语言,因此它并不意味着包含if/ else样式可执行语句:这是您正在使用的编程语言的责任.

Ruby中用于确定从YAML文件输出哪个配置字符串的简单示例可以定义您的YAML配置文件,如下所示:

data.yml

attributes:
  shipping_comment: Shipping comment / Instructions
  shipping_date: Date
Run Code Online (Sandbox Code Playgroud)

然后,在您的程序中,读取文件并在那里运行条件:

shipping.rb

#!/usr/bin/env ruby
require 'yaml'
config = YAML.load_file('data.yml')

attribute = true # your attribute to check here

if attribute
  puts config['attributes']['shipping_comment']
else
  puts config['attributes']['shipping_date']
end
Run Code Online (Sandbox Code Playgroud)