Every to_yaml output have three leading dashes:
---
a:
b:
c: soemthing
How convert object to yaml without leading dashes?
Every to_yaml output have three leading dashes:
---
a:
b:
c: soemthing
How convert object to yaml without leading dashes?
Under the hood, to_yaml
uses Psych to parse and emit your data.
Though there are optional parameters you can specify (listed here), none suppress the leading dashes.
The simplest approach would be to gsub
away the dashes.
object.to_yaml.gsub(/^---$/, "")
or, since Ruby 2.4, you can use delete_prefix
, which reads a little better. I also gets rid of the remaining \n
:
object.to_yaml.delete_prefix!("---\n")
From the YAML specification:
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.
That means: Your example starts as a valid YAML document should start. Nothing wrong with it.
© 2022 - 2024 — McMap. All rights reserved.