0

I have a yaml file that is valid and includes this snippet:

code:
-
  >
  <title> Jane's Website </title>

Which is parsed as an array:

"code": [
    "<title> Jane's Website </title>"
  ]

How do I get the psych library to write the yaml this way?

  • There's no easy way to do this, but it is possible. See these related questions: http://stackoverflow.com/questions/9640277/how-to-dump-strings-in-yaml-using-literal-scalar-style (this concerns literal scalar style ("`|`"); whereas you want folded ("`>`") but that's a very small difference); and http://stackoverflow.com/questions/11798716/psych-doc-says-at-the-mid-level-is-building-an-ast-but-how-exactly – Jordan Running Nov 25 '15 at 05:37

1 Answers1

1

You can't, AFAIK, because there are several ways to represent the same data in YAML, and Psych will pick one of them. I.e.

---
code:
- "<title> Jane's Website </title>"

is equivalent to your

code:
-
  >
  <title> Jane's Website </title>

and I don't believe you get to pick which one gets output. However, you should not rely on a particular form - if you are using YAML, any YAML that represents your structure should be accepted equally.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Do you have any idea on the reason why there are multiple ways to represent the same data in YAML? Seems like a unnecessary complication. – Kelvin Ma Nov 25 '15 at 16:26
  • Convenience, I guess - YAML was made so that it is easily writable by hand, and people appreciate options. For example, most strings in YAML do not need quotes; but some do, and having multiple ways of representing strings helps, as you can choose the most readable for the specific case. – Amadan Nov 25 '15 at 17:11