在 YAML 中存储信息

发布于 2024-09-05 03:25:06 字数 1366 浏览 5 评论 0原文

我查看了 YAML for ruby​​ 文档,但找不到答案。

我有一份几名员工的名单。每个人都有姓名、电话和电子邮件:

Employees:
    Name | Phone | Email
    john   111     [email protected]
    joe    123     [email protected]
    joan   321     [email protected]

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

employees = [ {:name => 'john', :phone => '111', :email => '[email protected]'}, {:name => 'joe', :phone => '123', :email => '[email protected]'}, {:name => 'joan', :phone => '321', :email => '[email protected]'} ]

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

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

谢谢!

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     [email protected]
    joe    123     [email protected]
    joan   321     [email protected]

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

employees = [ {:name => 'john', :phone => '111', :email => '[email protected]'}, {:name => 'joe', :phone => '123', :email => '[email protected]'}, {:name => 'joan', :phone => '321', :email => '[email protected]'} ]

This is how I parse the YAML file:

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

Thank you!

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

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

发布评论

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

评论(2

鱼忆七猫命九 2024-09-12 03:25:07
- name: john
  phone: 111
  email: [email protected]
- name: joe
  phone: 123
  email: [email protected]
- name: joan
  phone: 321
  email: [email protected]

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

- name: john
  phone: 111
  email: [email protected]
- name: joe
  phone: 123
  email: [email protected]
- name: joan
  phone: 321
  email: [email protected]

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

望喜 2024-09-12 03:25:07

请注意,Ruby 1.8.6 附带的标准 yaml UTF / Unicode 安全。

如果您要进行任何非 ASCII 测试,请使用 Ruby 1.9 yaml 或 Ya2YAML gem。

此外,查看 yaml 输入应该是什么的最简单方法是创建(在 Ruby irb 中)数据结构的示例。然后使用 .to_yaml 对象类方法将其转换为 yaml 文本。
例如

require 'yaml'
# create your structure
a = [{'name' => "Larry K", 'age' => 24,
     'job' => {'fulltime' => true, 'name' => 'engineer'}}]
a.to_yaml

=> "--- \n- name: Larry K\n  job: \n    name: engineer\n    fulltime: true\n  age: 24\n"

# then substitute line breaks for the \n:
--- 
- name: Larry K
  job: 
    name: engineer
    fulltime: true
  age: 24

Note that standard yaml which ships with Ruby 1.8.6 is NOT UTF / Unicode safe.

Use either Ruby 1.9 yaml or Ya2YAML gem if you'll have any non-ASCII test.

Also, the easiest way to see what the yaml input should be is to create (in Ruby irb), an example of your data structure. Then turn it into yaml text by using the .to_yaml object class method.
Eg

require 'yaml'
# create your structure
a = [{'name' => "Larry K", 'age' => 24,
     'job' => {'fulltime' => true, 'name' => 'engineer'}}]
a.to_yaml

=> "--- \n- name: Larry K\n  job: \n    name: engineer\n    fulltime: true\n  age: 24\n"

# then substitute line breaks for the \n:
--- 
- name: Larry K
  job: 
    name: engineer
    fulltime: true
  age: 24
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文