Skip to content

Commit

Permalink
Merge pull request #641 from Invoca/quote-Y-N
Browse files Browse the repository at this point in the history
issue #443: quote Y and N when dumping
  • Loading branch information
tenderlove authored Jan 17, 2024
2 parents fc9ca15 + 6750b35 commit 0ebd571
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def visit_String o
tag = 'tag:yaml.org,2002:str'
plain = false
quote = false
elsif o == 'y' || o == 'n'
elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N'
style = Nodes::Scalar::DOUBLE_QUOTED
elsif @line_width && o.length > @line_width
style = Nodes::Scalar::FOLDED
Expand Down
12 changes: 6 additions & 6 deletions test/psych/test_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ def initialize
end
end

# 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in
# 'y', 'Y', 'n', 'N' are kind of ambiguous. Syck treated those 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"))
def test_all_yaml_1_1_booleans_are_quoted
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
yaml_1_1_booleans.each do |boolean|
assert_match(/"#{boolean}"|'#{boolean}'/, Psych.dump(boolean))
end
end

def test_string_with_newline
Expand Down

0 comments on commit 0ebd571

Please sign in to comment.