Skip to content

Commit 6a1c306

Browse files
committed
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
1 parent 0be4978 commit 6a1c306

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/psych/visitors/yaml_tree.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ def visit_String o
272272
tag = 'tag:yaml.org,2002:str'
273273
plain = false
274274
quote = false
275+
elsif o == 'y' || o == 'n'
276+
style = Nodes::Scalar::DOUBLE_QUOTED
275277
elsif @line_width && o.length > @line_width
276278
style = Nodes::Scalar::FOLDED
277279
elsif o =~ /^[^[:word:]][^"]*$/

test/psych/test_string.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ def initialize
1717
end
1818
end
1919

20+
# 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in
21+
# YAML documents as strings. But this is not what the YAML 1.1 spec says.
22+
# YAML 1.1 says they should be treated as booleans. When we're dumping
23+
# documents, we know it's a string, so adding quotes will eliminate the
24+
# "ambiguity" in the emitted document
25+
def test_y_is_quoted
26+
assert_match(/"y"/, Psych.dump("y"))
27+
end
28+
29+
def test_n_is_quoted
30+
assert_match(/"n"/, Psych.dump("n"))
31+
end
32+
2033
def test_string_with_newline
2134
assert_equal "1\n2", Psych.load("--- ! '1\n\n 2'\n")
2235
end

0 commit comments

Comments
 (0)