Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an empty array in YAML?

Tags:

arrays

ruby

yaml

array_with_three_elements:
- 1
- 2
- 3

empty_array:

Is there any way to specify that empty_array: is an array with no elements, such as with []? When I load it into a ruby hash I'd like it to know that it's an array.

Thanks

like image 979
Julian Mann Avatar asked Feb 24 '11 20:02

Julian Mann


People also ask

How declare empty array in YAML?

Thus [] works for an empty sequence, "" works for an empty string, and {} works for an empty mapping. Many parsers are still on YAML 1.1; this is probably what Wikipedia is talking about.

How do I create an array 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. '

Can you create an empty array?

To create an empty array, you can use an array initializer. The length of the array is equal to the number of items enclosed within the braces of the array initializer. Java allows an empty array initializer, in which case the array is said to be empty.

How add empty string in YAML?

You can use ~ or null . Show activity on this post. If you want an empty string, rather than a null value, you can use two single quotes.


1 Answers

Try using [], like:

empty_array: []

So in Ruby you have:

x = YAML::load("empty_array: []")
x # => {"empty_array" => []}
like image 94
maerics Avatar answered Oct 06 '22 01:10

maerics