diff --git a/rational.c b/rational.c index 4bdc695dc1..742f594f65 100644 --- a/rational.c +++ b/rational.c @@ -2396,6 +2396,7 @@ read_rat_nos(const char **s, int sign, int strict, VALUE *num) if (**s == '/') { (*s)++; if (!read_digits(s, strict, &den, NULL)) goto failed; + if (den == ZERO) rb_num_zerodiv(); nurat_reduce(num, &den); if (div != ONE && den != ONE) den = rb_int_mul(den, div); diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 6185611a5c..ed4c5b1006 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -776,6 +776,11 @@ class Rational_Test < Test::Unit::TestCase ng[5, 3, '5/3x'] end + def test_parse_zero_denominator + assert_raise(ZeroDivisionError) {"1/0".to_r} + assert_raise(ZeroDivisionError) {Rational("1/0")} + end + def test_to_i assert_equal(1, Rational(3,2).to_i) assert_equal(1, Integer(Rational(3,2)))