diff --git a/ext/-test-/rational/rat.c b/ext/-test-/rational/rat.c index 01388346f7..b1ffbda144 100644 --- a/ext/-test-/rational/rat.c +++ b/ext/-test-/rational/rat.c @@ -29,9 +29,17 @@ gcd_gmp(VALUE x, VALUE y) #define gcd_gmp rb_f_notimplement #endif +static VALUE +s_rational_raw(VALUE klass, VALUE x, VALUE y) +{ + return rb_rational_raw(x, y); +} + void Init_rational(VALUE klass) { rb_define_method(rb_cInteger, "gcd_normal", gcd_normal, 1); rb_define_method(rb_cInteger, "gcd_gmp", gcd_gmp, 1); + + rb_define_singleton_method(rb_cRational, "raw", s_rational_raw, 2); } diff --git a/rational.c b/rational.c index f483c6d68b..0261c6940a 100644 --- a/rational.c +++ b/rational.c @@ -1927,6 +1927,10 @@ rb_gcdlcm(VALUE self, VALUE other) VALUE rb_rational_raw(VALUE x, VALUE y) { + if (INT_NEGATIVE_P(y)) { + x = rb_int_uminus(x); + y = rb_int_uminus(y); + } return nurat_s_new_internal(rb_cRational, x, y); } diff --git a/test/-ext-/rational/test_rat.rb b/test/-ext-/rational/test_rat.rb index 626ffb9661..dbba00ca61 100644 --- a/test/-ext-/rational/test_rat.rb +++ b/test/-ext-/rational/test_rat.rb @@ -29,4 +29,18 @@ class TestRational < Test::Unit::TestCase rescue NotImplementedError end end + + def test_rb_rational_raw + rat = Rational.raw(1, 2) + assert_equal(1, rat.numerator) + assert_equal(2, rat.denominator) + + rat = Rational.raw(-1, 2) + assert_equal(-1, rat.numerator) + assert_equal(2, rat.denominator) + + rat = Rational.raw(1, -2) + assert_equal(-1, rat.numerator) + assert_equal(2, rat.denominator) + end end