diff --git a/rational.c b/rational.c index 53e136ff13..43c49350f6 100644 --- a/rational.c +++ b/rational.c @@ -2356,7 +2356,7 @@ static VALUE parse_rat(const char *s, const char *const e, int strict) { int sign; - VALUE num, den, ndiv; + VALUE num, den, ndiv, ddiv; s = skip_ws(s, e); sign = read_sign(&s, e); @@ -2368,12 +2368,8 @@ parse_rat(const char *s, const char *const e, int strict) nurat_reduce(&num, &ndiv); den = ndiv; if (s < e && *s == '/') { - char *t; s++; - den = rb_int_parse_cstr(s, e-s, &t, NULL, - 10, RB_INT_PARSE_UNDERSCORE); - s = t; - if (NIL_P(den)) { + if (!read_num(&s, e, &den, &ddiv)) { if (strict) return Qnil; den = ndiv; } @@ -2384,8 +2380,11 @@ parse_rat(const char *s, const char *const e, int strict) return Qnil; } else { + nurat_reduce(&den, &ddiv); nurat_reduce(&num, &den); - den = rb_int_mul(den, ndiv); + nurat_reduce(&ndiv, &ddiv); + if (ndiv != ONE) den = rb_int_mul(den, ndiv); + if (ddiv != ONE) num = rb_int_mul(num, ddiv); } } else if (strict && skip_ws(s, e) != e) { diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 43bd750615..be90bf85bf 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -747,6 +747,19 @@ class Rational_Test < Test::Unit::TestCase ng[ 5, 1, '5e_1'] ng[50, 1, '5e1_'] + ok[ 50, 33, '5/3.3'] + ok[ 5, 3, '5/3e0'] + ok[ 5, 30, '5/3e1'] + ng[ 5, 3, '5/3._3'] + ng[ 50, 33, '5/3.3_'] + ok[500,333, '5/3.3_3'] + ng[ 5, 3, '5/3e'] + ng[ 5, 3, '5/3_e'] + ng[ 5, 3, '5/3e_'] + ng[ 5, 3, '5/3e_1'] + ng[ 5, 30, '5/3e1_'] + ok[ 5, 300000000000, '5/3e1_1'] + ng[0, 1, ''] ng[0, 1, ' '] ng[5, 1, "\f\n\r\t\v5\0"] @@ -756,7 +769,6 @@ class Rational_Test < Test::Unit::TestCase ng[5, 1, '5x'] ng[5, 1, '5/_3'] ng[5, 3, '5/3_'] - ng[5, 3, '5/3.3'] ng[5, 3, '5/3x'] end