Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why are the ANSI escape chars in this YAML data not being evaluated when output to terminal by my Ruby script?

I have this YAML data:

data.yml

game:
  welcome: Welcome to \e[34myour\e[0m new chess game players.\n\n

which I am successfully accessing with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

puts data['game']['welcome']

however when output to terminal neither the ansi escape character formatting nor the newline formatting is working. It just prints out all of the characters literally. I have tried/checked:

  • terminal is using UTF-8 encoding and does print ansi escape formatting when directly used on string characters on a ruby programme.
  • tried escaping the escape characters with another \ character, e.g. \e[34myour\e[0m. This doesn’t work.

Any ideas how I might be able to get this working? Thanks

>Solution :

YAML only allows escape character sequences in double-quoted scalars. See section 5.7 Escaped Characters of the YAML specification [bold italic emphasis mine]:

5.7. Escaped Characters

All non-printable characters must be escaped. YAML escape sequences use the “\” notation common to most modern computer languages. Each escape sequence must be parsed into the appropriate Unicode character. The original escape sequence is a presentation detail and must not be used to convey content information.

Note that escape sequences are only interpreted in double-quoted scalars. In all other scalar styles, the “\” character has no special meaning and non-printable characters are not available.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading