有条件地检查 yaml 文件以显示正确的内容 [英] Conditional check in yaml file to show the proper content

查看:19
本文介绍了有条件地检查 yaml 文件以显示正确的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查 yaml 文件中的 if/else.

How can I check if / else in yaml file.

喜欢:

 if %{attribute}
    attributes:
       shipping_comment: Shipping comment / Instructions
 else
    attributes:
       shipping_date: Date

推荐答案

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

YAML is a data serialisation language, so it's not meant to contain if/else style executable statements: that's the responsibility of the programming language you're using.

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

A simple example in Ruby to determine which config string from a YAML file to output could be defining your YAML config file as follows:

data.yml

attributes:
  shipping_comment: Shipping comment / Instructions
  shipping_date: Date

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

Then, in your program, read the file in and run the conditional there:

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

这篇关于有条件地检查 yaml 文件以显示正确的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆