Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple keys for one value in yaml

Tags:

arrays

yaml

key

Is it possible to use different keys for the same value?

[activerecord, activemodel]: 'test' 

I expect the same result as with this:

activerecord: 'test' activemodel: 'test' 
like image 634
antpaw Avatar asked Nov 23 '10 10:11

antpaw


People also ask

How do I give multiple values in YAML?

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. '

Does YAML allow duplicate keys?

Duplicate keys in YAML files are not allowed in the spec (https://yaml.org/spec/1.2.2/#nodes, https://yaml.org/spec/1.0/#model-node), but the older version of symfony/yaml does not complain about them.

Can YAML keys have spaces?

Spaces are allowed in keys according to the specification and for those you don't need quotes (double or single, each with their own use). It is just a scalar string containing a space.

Which element of YAML defines a key-value pair?

2. Key-Value Pairs. The YAML above defines four keys - first_name , last_name , age_years , and home - with their four respective values. Values can be character strings, numeric (integer, floating point, or scientfic representation), Boolean ( true or false ), or more complex nested types (see below).


1 Answers

That doesn't work because YAML allows you to specify keys of any type, so

[activerecord, activemodel]: 'test' 

is a mapping with a single key, the sequence [activerecord, activemodel] whose value is 'test'.

Instead, you can use an anchor/alias:

activerecord: &my_value 'test' activemodel: *my_value 

However, there's no way of attaching both keys to the single value in one key/value pair.

like image 81
Jesse Beder Avatar answered Oct 04 '22 05:10

Jesse Beder