Skip to content

Commit 93c8fb4

Browse files
committed
issue #443: quote Y and N when dumping
1 parent 9de928f commit 93c8fb4

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/psych/visitors/yaml_tree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def visit_String o
272272
tag = 'tag:yaml.org,2002:str'
273273
plain = false
274274
quote = false
275-
elsif o == 'y' || o == 'n'
275+
elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N'
276276
style = Nodes::Scalar::DOUBLE_QUOTED
277277
elsif @line_width && o.length > @line_width
278278
style = Nodes::Scalar::FOLDED

test/psych/test_string.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ def initialize
2424
# "ambiguity" in the emitted document
2525
def test_y_is_quoted
2626
assert_match(/"y"/, Psych.dump("y"))
27+
assert_match(/"Y"/, Psych.dump("Y"))
2728
end
2829

2930
def test_n_is_quoted
3031
assert_match(/"n"/, Psych.dump("n"))
32+
assert_match(/"N"/, Psych.dump("N"))
33+
end
34+
35+
def test_all_yaml_1_1_booleans_are_quoted
36+
yaml_1_1_booleans = %w[y Y yes Yes YES n N no No NO true True TRUE false False FALSE on On ON off Off OFF] # from https://yaml.org/type/bool.html
37+
yaml_1_1_booleans.each do |boolean|
38+
assert_match(/"#{boolean}"|'#{boolean}'/, Psych.dump(boolean))
39+
end
3140
end
3241

3342
def test_string_with_newline

0 commit comments

Comments
 (0)