In the mysterious language of YAML, when we say "sequences", we're actually talking about what you might know as "lists". Similar to a crew of pirates on a ship, elements in a YAML sequence sail in order, one following the other.
There are two ways to express these sequences:
- Inline Style
- Block Style
Let's cover each of these below:
Inline Style
The inline style is like a quick treasure map drawn in the sand with a stick. It's simple, compact, and you can see everything at a glance.
In the inline style, the sequence items are enclosed in square brackets [
]
, and separated by commas ,
.
You can represent a list of your favorite fruits with inline style, like this:
favorite_fruits: ["apple", "banana", "cherry"]
In this example, "apple", "banana", and "cherry" are elements of the sequence and the entire list is on one line. The brackets tell YAML, "Hey! There's a list here!", and the commas are like little flags marking where one element ends and the next one begins.
Block Style
The block style, on the other hand, is more like a detailed treasure map on a large, sturdy piece of parchment. Each item gets its own line, making it easier to read when you have a lot of items.
In the block style, each item in the sequence is on a new line starting with a hyphen -
and a space.
Here is how you can represent the same list of your favorite fruits in the block style:
favorite_fruits:
- apple
- banana
- cherry
In this example, each fruit gets its own line, and they are all neatly ordered, just like a well-disciplined pirate crew! The hyphen -
acts as a little flag, signaling the start of a new item.
The Adventure Continues
And there you have it, matey! You've successfully charted the waters of YAML sequences. But don't get too comfortable - there are many more mysteries and treasures awaiting you in the vast sea of YAML.