From 8a3710a6b1ea4c5163a468b3d821e65026059eab Mon Sep 17 00:00:00 2001 From: SHIBATA Hiroshi Date: Wed, 29 Nov 2017 15:23:06 +0900 Subject: [PATCH] Revert "Revert "Double quote stings that contain single quotes only"" --- lib/psych/visitors/yaml_tree.rb | 2 +- test/psych/test_string.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index b6c86f4c..58394118 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -311,7 +311,7 @@ def visit_String o style = Nodes::Scalar::DOUBLE_QUOTED elsif @line_width && o.length > @line_width style = Nodes::Scalar::FOLDED - elsif o.match?(/^[^[:word:]][^"]*$/) + elsif o.match?(/^[^[:word:]][^"]*$/) || o.match?(/^([^"]*'+[^"]*)+$/) style = Nodes::Scalar::DOUBLE_QUOTED elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o) style = Nodes::Scalar::SINGLE_QUOTED diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index cfd235a5..47e4303a 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -60,6 +60,13 @@ def test_single_quote_when_matching_date RUBY end + def test_doublequotes_when_there_are_single_quotes_only + str = "psych: Please don't escape ' with ' here." + yaml = Psych.dump str + assert_equal "--- \"psych: Please don't escape ' with ' here.\"\n", yaml + assert_equal str, Psych.load(yaml) + end + def test_plain_when_shorten_than_line_width_and_no_final_line_break str = "Lorem ipsum" yaml = Psych.dump str, line_width: 12