Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a list of associative array in yaml

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

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:

undefined method `symbolize_keys!' for nil: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 ?

like image 657
Antzi Avatar asked Jun 18 '13 16:06

Antzi


People also ask

How do I create an array in YAML file?

An array is a group of similar values with a single name. In YAML, Array represents a single key mapped to multiple values. Each value starts with a hyphen - symbol followed by space. In a single line, write the same thing above using 'square brackets syntax.

How do you represent an array in YAML?

The block sequence style of YAML uses hyphens or dashes to ( - ) to represent arrays. A hyphen ( - ) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level.

How do I specify a list in YAML?

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... . This is part of the YAML format and indicates the start and end of a document. All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):

Does YAML support arrays?

An array can contain any valid YAML value. The values in a list do not have to be the same type.


1 Answers

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

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.

like image 70
Shadwell Avatar answered Sep 30 '22 14:09

Shadwell