Rails object.to_yaml without leading dashes
Asked Answered
K

2

6

Every to_yaml output have three leading dashes:

---
a:
  b:
    c: soemthing

How convert object to yaml without leading dashes?

Kippar answered 19/6, 2014 at 10:4 Comment(3)
Yes, it works but i search for more convenient solution.Kippar
What is unconveinent about it? It's unclear what you are looking for.Sinistrodextral
It's kind of "magick numbers". Not beutiful and non convenient.Kippar
C
2

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")
Calm answered 18/7, 2022 at 17:59 Comment(0)
P
-1

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.

Portingale answered 19/6, 2014 at 12:5 Comment(2)
I just want generate part of document. My case - yaml writer with reusable blocks (&identifier) detection.Kippar
This doesn't answer the original question.Lardy

© 2022 - 2024 — McMap. All rights reserved.