rubyyamlansi-escape

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:

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:

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.