From 8579a33966f49e600cec0589a918743175f4ec50 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 5 Sep 2013 17:39:53 +0000 Subject: [PATCH] * ext/psych/lib/psych/visitors/yaml_tree.rb: use double quotes when strings start with special characters. [Fixes GH-157] https://github.com/tenderlove/psych/issues/157 * test/psych/test_string.rb: test for change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42850 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ ext/psych/lib/psych/visitors/yaml_tree.rb | 2 ++ test/psych/test_string.rb | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/ChangeLog b/ChangeLog index 120a25aa92..443575c37f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Fri Sep 6 02:37:22 2013 Aaron Patterson + + * ext/psych/lib/psych/visitors/yaml_tree.rb: use double quotes when + strings start with special characters. + [Fixes GH-157] https://github.com/tenderlove/psych/issues/157 + + * test/psych/test_string.rb: test for change. + Fri Sep 6 00:05:14 2013 Nobuyoshi Nakada * class.c (rewrite_cref_stack): remove recursion. diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index eb1f0d76a7..f8bece64b6 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -288,6 +288,8 @@ module Psych quote = false elsif o =~ /\n/ style = Nodes::Scalar::LITERAL + elsif o =~ /^\W/ + style = Nodes::Scalar::DOUBLE_QUOTED else unless String === @ss.tokenize(o) style = Nodes::Scalar::SINGLE_QUOTED diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 2512bb6802..025ef9564e 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -15,6 +15,11 @@ module Psych end end + def test_doublequotes_when_there_is_a_single + yaml = Psych.dump "@123'abc" + assert_match(/---\s*"/, yaml) + end + def test_dash_dot assert_cycle '-.' assert_cycle '+.'