如何在yaml中创建关联数组的列表

时间:2023-01-15 00:11:49

I'm trying to store some configuration variables in yaml represented as an associative array aka dictionary. Here is how I did:

我试图在yaml中存储一些配置变量,yaml表示为关联数组,即dictionary。我是这样做的:

content_prices:                                                                                                                                                                                                                               
  - {country: AU, price: 6990000}                                                                                                                                                                                                             
  - {country: AT, price: 4990000}                                                                                                                                                                                                             
  - {country: BE, price: 4990000}  

This produce an exception when I try to parse it from my ROR init files:

当我试图从ROR init文件中解析它时,会产生一个异常:

undefined method `symbolize_keys!' for nil:NilClass

未定义的方法“symbolize_keys !”零:NilClass

Here is how I init it:

我是这样初始化的:

Config = YAML.load_file("#{Rails.root}/config/prices.yml")[Rails.env].symbolize_keys!

I guess my yaml syntax is wrong, then how to write it properly ?

我猜我的yaml语法是错的,那怎么写好呢?

2 个解决方案

#1


144  

Your YAML looks okay, or you can configure an array of hashes like this :

您的YAML看起来还可以,或者您可以配置一个散列数组,如下所示:

content_prices:
  - country: AU
    price: 6990000
  - country: AT
    price: 4990000
  - country: BE
    price: 4990000

Which will load as the following hash:

将以以下散列方式加载:

{"content_prices"=>[
  {"country"=>"AU", "price"=>6990000}, 
  {"country"=>"AT", "price"=>4990000}, 
  {"country"=>"BE", "price"=>4990000}]}

But that still doesn't give you any reference to the Rails.env in the main hash. The problem seems to be what you're expecting to be in your hash rather than the format of the YAML.

但是这仍然没有给你任何关于Rails的参考。env在主散列中。问题似乎是您期望的哈希格式,而不是YAML的格式。

#2


11  

Not on rails, but on Symfony2 php, I had to configure the yml file like this:

不是在rails上,而是在Symfony2 php上,我必须这样配置yml文件:

content_prices:
  - 
    country: AU
    price: 6990000
  - 
    country: AT
    price: 4990000
  - 
    country: BE
    price: 4990000

#1


144  

Your YAML looks okay, or you can configure an array of hashes like this :

您的YAML看起来还可以,或者您可以配置一个散列数组,如下所示:

content_prices:
  - country: AU
    price: 6990000
  - country: AT
    price: 4990000
  - country: BE
    price: 4990000

Which will load as the following hash:

将以以下散列方式加载:

{"content_prices"=>[
  {"country"=>"AU", "price"=>6990000}, 
  {"country"=>"AT", "price"=>4990000}, 
  {"country"=>"BE", "price"=>4990000}]}

But that still doesn't give you any reference to the Rails.env in the main hash. The problem seems to be what you're expecting to be in your hash rather than the format of the YAML.

但是这仍然没有给你任何关于Rails的参考。env在主散列中。问题似乎是您期望的哈希格式,而不是YAML的格式。

#2


11  

Not on rails, but on Symfony2 php, I had to configure the yml file like this:

不是在rails上,而是在Symfony2 php上,我必须这样配置yml文件:

content_prices:
  - 
    country: AU
    price: 6990000
  - 
    country: AT
    price: 4990000
  - 
    country: BE
    price: 4990000