The following…
require 'yaml' test = 'I'm a b&d string' File.open('test.yaml', 'w') do |out| out.write(test.to_yaml) end
…outputs …
--- this is a b&d string
How can I get it to output
--- 'this is a b&d string'
???
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to store an escaped string in YAML, escape it using
#inspectbefore you convert it to YAML:YAML doesn’t quote strings unless it has to. It quotes strings if they include things that it would miss if it stored it unquoted – like surrounding quote characters or trailing or leading spaces:
However, as a YAML consumer, whether the string is quoted shouldn’t matter to you. You should never be parsing the YAML text yourself – leave that to the libraries. If you need the string to be quoted in the YAML file, that smells bad to me.
It doesn’t matter whether your strings have ‘&’s in them, YAML will preserve the string: