I am modifying a YAML file in Ruby. After I write back the modified YAML, I see a ---
added on top of the file. How is this getting added and how do I get rid of it?
This uses yq from https://kislyuk.github.io/yq/ to delete the top-level /image-content section from the YAML document using the del() command. Redirect this to a new file if you want to save it, or use the --in-place option to do in-place editing (after testing without that option first, of course).
The file starts with three dashes. These dashes indicate the start of a new YAML document. YAML supports multiple documents, and compliant parsers will recognize each set of dashes as the beginning of a new one.
YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present. Also, I tried a sample without --- and understood that it is not mandatory to have them.
these lines will be indented by 4 spaces. The greater than sign notation, also referred to as “folded block”: folded: > This block of text will be the value of 'folded', but this. time, all newlines will be replaced with a single space. Blank lines, like above, are converted to a newline character.
YAML spec says:
YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present.
Example:
# Ranking of 1998 home runs --- - Mark McGwire - Sammy Sosa - Ken Griffey # Team ranking --- - Chicago Cubs - St Louis Cardinals
So if you have multiple documents per YAML file, you have to separate them by three dashes. If you only have one document, you can remove/omit it (I never had a problem with YAML in ruby if three-dashes was missing). The reason why it's added when you yamlify your object is that, I guess, the dumper is written "by the spec" and doesn't care to implement such "shortcuts" (omit three-dashes when it's only one document).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With