【问题标题】:Retrieve array data from yaml file Ruby从 yaml 文件 Ruby 中检索数组数据
【发布时间】:2015-04-30 09:47:08
【问题描述】:

我在 yaml 文件中有以下数组:

error_messages:
  - 'Error! There is no registration number'
  - 'Error! Event is not added'

我正在从这个文件中读取数据如下:

$common_test_data['error_messages[0]']

我已经在全局变量中初始化了我的 yaml 文件:

$common_test_data =  YAML.load_file("#{Dir.pwd}/test_data/common_data.yml")

但不幸的是,没有从 yaml 数组中检索到数据。也许有特定的方法可以从 yaml 文件中检索数据?

【问题讨论】:

    标签: ruby arrays yaml


    【解决方案1】:

    您应该通过以下方式检索它:
    $common_test_data['error_messages'][0]

    $common_test_data 是一个哈希。
    $common_test_data['error_messages'] 是一个数组。

    【讨论】:

    • 那你可以选择我的unswer作为解决方案:-)
    • 我的声望还太低,对不起)
    • @ВасисуалияЛоханкина 这是暂时的问题☺
    猜你喜欢
    • 1970-01-01
    • 2016-06-28
    • 2023-03-16
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多