YAML 条目可以用作同一文件中条目的子集吗?

发布于 2024-09-16 01:16:56 字数 386 浏览 2 评论 0原文

我在 Rails 应用程序中使用 YAML 文件进行配置,文件中的一些条目是未来条目的子集,例如 -

域名:domain.com
姓名:杰克
电子邮件:[电子邮件受保护]

为了提高效率, YAML 中可能有类似的事情吗?

域名:domain.com
姓名:杰克
电子邮件:jack@${domain}

I am using a YAML file for configuration in my rails app and a few entries in the file are subsets of future entries, eg -

domain: domain.com
name: jack
email: [email protected]

To make this more efficient, is something like this possible in YAML ?

domain: domain.com
name: jack
email: jack@${domain}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-09-23 01:16:57

YAML 有 ,但你不能完全将它们用作变量就像你在问题中所做的那样。您可以通过后处理步骤来接近。假设你的 domains.yaml

domains:
  - domain: &domain domain.com
    name: jack
    emaillocal: jack
    emaildomain: *domain

  - domain: &domain thehill.com
    name: Jill
    emaillocal: jill
    emaildomain: *domain

然后

#! /usr/bin/ruby

require "yaml"

yaml = YAML.load_file('domains.yaml')

yaml['domains'].each { |d|
  d['email'] = d['emaillocal'] + '@' + d['emaildomain']
}

puts YAML::dump(yaml)

你就得到了

domains: 
- name: jack
  emaildomain: domain.com
  domain: domain.com
  emaillocal: jack
  email: [email protected]
- name: Jill
  emaildomain: thehill.com
  domain: thehill.com
  emaillocal: jill
  email: [email protected]

YAML has anchors, but you can't quite use them as variables the way you did in your question. You can get close with a post-processing step. Say your domains.yaml is

domains:
  - domain: &domain domain.com
    name: jack
    emaillocal: jack
    emaildomain: *domain

  - domain: &domain thehill.com
    name: Jill
    emaillocal: jill
    emaildomain: *domain

Then with

#! /usr/bin/ruby

require "yaml"

yaml = YAML.load_file('domains.yaml')

yaml['domains'].each { |d|
  d['email'] = d['emaillocal'] + '@' + d['emaildomain']
}

puts YAML::dump(yaml)

you get

domains: 
- name: jack
  emaildomain: domain.com
  domain: domain.com
  emaillocal: jack
  email: [email protected]
- name: Jill
  emaildomain: thehill.com
  domain: thehill.com
  emaillocal: jill
  email: [email protected]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文