From cf82e485d1511330c0607612c24712255739df49 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 9 Jan 2013 11:28:15 -0800 Subject: [PATCH] * ext/psych/lib/psych/scalar_scanner.rb: strip trailing dots from floats so that Float() will not raise an exception. * test/psych/test_numeric.rb: test to ensure "1." can be loaded * test/psych/test_string.rb: make sure "1." can round trip fixes #109 --- CHANGELOG.rdoc | 9 +++++++++ lib/psych/scalar_scanner.rb | 2 +- test/psych/test_numeric.rb | 4 ++++ test/psych/test_string.rb | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 291ce976..98e3524e 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,12 @@ +Thu Jan 10 04:23:07 2013 Aaron Patterson + + * ext/psych/lib/psych/scalar_scanner.rb: strip trailing dots from + floats so that Float() will not raise an exception. + + * test/psych/test_numeric.rb: test to ensure "1." can be loaded + + * test/psych/test_string.rb: make sure "1." can round trip + Sat Nov 17 12:03:41 2012 Aaron Patterson * ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when diff --git a/lib/psych/scalar_scanner.rb b/lib/psych/scalar_scanner.rb index d0beee36..57594599 100644 --- a/lib/psych/scalar_scanner.rb +++ b/lib/psych/scalar_scanner.rb @@ -96,7 +96,7 @@ def tokenize string @string_cache[string] = true string else - Float(string.gsub(/[,_]/, '')) + Float(string.gsub(/[,_]|\.$/, '')) end else int = parse_int string.gsub(/[,_]/, '') diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb index 200a9f01..0858e799 100644 --- a/test/psych/test_numeric.rb +++ b/test/psych/test_numeric.rb @@ -16,6 +16,10 @@ def teardown $DEBUG = @old_debug end + def test_load_float_with_dot + assert_equal 1.0, Psych.load('--- 1.') + end + def test_non_float_with_0 str = Psych.load('--- 090') assert_equal '090', str diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 0c5d4d26..aa6866b6 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -102,6 +102,10 @@ def test_binary assert_cycle string end + def test_float_confusion + assert_cycle '1.' + end + def binary_string percentage = 0.31, length = 100 string = '' (percentage * length).to_i.times do |i|