1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/psych] Add quotes to the strings "y" and "n"

'y' and 'n' are kind of ambiguous.  Syck treated y and n literals in
YAML documents as strings.  But this is not what the YAML 1.1 spec says.
YAML 1.1 says they should be treated as booleans.  When we're dumping
documents, we know it's a string, so adding quotes will eliminate the
"ambiguity" in the emitted document

Fixes #443

6a1c30634e
This commit is contained in:
Aaron Patterson 2021-08-04 09:27:04 -07:00 committed by Hiroshi SHIBATA
parent 0925fddc80
commit 9ed2cb26de
2 changed files with 15 additions and 0 deletions

View file

@ -272,6 +272,8 @@ module Psych
tag = 'tag:yaml.org,2002:str'
plain = false
quote = false
elsif o == 'y' || o == 'n'
style = Nodes::Scalar::DOUBLE_QUOTED
elsif @line_width && o.length > @line_width
style = Nodes::Scalar::FOLDED
elsif o =~ /^[^[:word:]][^"]*$/

View file

@ -17,6 +17,19 @@ module Psych
end
end
# 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in
# YAML documents as strings. But this is not what the YAML 1.1 spec says.
# YAML 1.1 says they should be treated as booleans. When we're dumping
# documents, we know it's a string, so adding quotes will eliminate the
# "ambiguity" in the emitted document
def test_y_is_quoted
assert_match(/"y"/, Psych.dump("y"))
end
def test_n_is_quoted
assert_match(/"n"/, Psych.dump("n"))
end
def test_string_with_newline
assert_equal "1\n2", Psych.load("--- ! '1\n\n 2'\n")
end