我在我的Rails應用程序中使用YAML文件配置和文件中的幾個項目都是未來項目的子集,例如: -可以將YAML條目用於同一文件中的條目子集嗎?
域名:domain.com
名稱:千斤頂
電子郵件:千斤頂@ domain.com
爲了使這個更有效率,是這樣的可能在YAML?
域名:domain.com
名稱:千斤頂
電子郵件:插孔@ $ {}域
我在我的Rails應用程序中使用YAML文件配置和文件中的幾個項目都是未來項目的子集,例如: -可以將YAML條目用於同一文件中的條目子集嗎?
域名:domain.com
名稱:千斤頂
電子郵件:千斤頂@ domain.com
爲了使這個更有效率,是這樣的可能在YAML?
域名:domain.com
名稱:千斤頂
電子郵件:插孔@ $ {}域
YAML有anchors,但你不能完全把它們作爲變量的方法你在你的問題上做了。您可以通過後期處理步驟來完成。說你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]