2010-08-21 6 views

Répondre

0

YAML a anchors, mais vous ne pouvez pas tout à fait les utiliser comme des variables de la façon dont vous fait dans votre question. Vous pouvez vous rapprocher avec une étape de post-traitement. Supposons que votre domains.yaml est

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

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

Puis, avec

#! /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) 

vous

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]
Questions connexes