How to remove '---' on top of a YAML file?

12,379

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

Share:
12,379

Related videos on Youtube

Ava
Author by

Ava

Beginner!

Updated on June 26, 2022

Comments

  • Ava
    Ava about 2 years

    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?

    • Raj
      Raj over 10 years
      showing some code might help
  • Ava
    Ava over 10 years
    I just have one document, how do I remove it every time I modify the file.
  • Sergio Tulentsev
    Sergio Tulentsev over 10 years
    You could just yaml.gsub("---\n", '').
  • Ava
    Ava over 10 years
    gsub is a method for strings. Getting undefined method `gsub' for #<Hash
  • Sergio Tulentsev
    Sergio Tulentsev over 10 years
    @Ava: yes, do hash.to_yaml.gsub(...)