Ruby-on-rails 如何在yaml中制作关联数组的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17173864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:11:11  来源:igfitidea点击:

How to make a list of associative array in yaml

ruby-on-railsdictionaryyamlassociative-array

提问by Antzi

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

我试图在 yaml 中存储一些配置变量,表示为关联数组又名字典。这是我的做法:

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 初始化文件解析它时,这会产生一个异常:

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 语法是错误的,那么如何正确编写它?

回答by Shadwell

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.envin 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 的格式。

回答by sinhix

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

回答by GrumpyHat

Just in case someone wants to use dynamic keys, it is also possible:

万一有人想使用动态密钥,也可以:

AppBundle\Service\MailerService:
    lazy: false
    arguments:
      $defaultFrom:
        '%mailer_user%': '%mailer_name%'