在 YAML 中存储信息 [英] Storing information inside YAML

查看:37
本文介绍了在 YAML 中存储信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了 YAML for ruby​​ 文档,但找不到答案.

I looked through the YAML for ruby documentation and couldn't find an answer.

我有几个员工的名单.每个人都有自己的姓名、电话和电子邮件地址:

I have a list of several employees. Each has a name, phone, and email as such:

Employees:
    Name | Phone | Email
    john   111     a@b.com
    joe    123     b@a.org
    joan   321     c@a.net

我将如何在 YAML 中编写上述信息以得到以下 ruby​​ 输出?

How would I write the above information in YAML to end up with the following ruby output?

employees = [ {:name => 'john', :phone => '111', :email => 'a@b.com'}, {:name => 'joe', :phone => '123', :email => 'b@a.org'}, {:name => 'joan', :phone => '321', :email => 'c@a.net'} ]

这是我解析 YAML 文件的方式:

This is how I parse the YAML file:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

谢谢!

推荐答案

- name: john
  phone: 111
  email: a@b.com
- name: joe
  phone: 123
  email: b@a.org
- name: joan
  phone: 321
  email: c@a.net

输出是字符串键,而不是符号键,但如果确实需要,您可以自己进行转换.

Output is string keys, not symbol keys, but you can make that conversion yourself if really necessary.

这篇关于在 YAML 中存储信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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