ruby-changes:59722
From: Kenta <ko1@a...>
Date: Fri, 17 Jan 2020 11:06:22 +0900 (JST)
Subject: [ruby-changes:59722] 47465ab1cc (master): rb_rational_raw: make a denominator always positive
https://git.ruby-lang.org/ruby.git/commit/?id=47465ab1cc From 47465ab1ccf48d2c905dbcf3b676e30b61cc41ca Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Fri, 17 Jan 2020 10:41:03 +0900 Subject: rb_rational_raw: make a denominator always positive diff --git a/ext/-test-/rational/rat.c b/ext/-test-/rational/rat.c index 0138834..b1ffbda 100644 --- a/ext/-test-/rational/rat.c +++ b/ext/-test-/rational/rat.c @@ -29,9 +29,17 @@ gcd_gmp(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/ext/-test-/rational/rat.c#L29 #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 f483c6d..0261c69 100644 --- a/rational.c +++ b/rational.c @@ -1927,6 +1927,10 @@ rb_gcdlcm(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1927 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 626ffb9..dbba00c 100644 --- a/test/-ext-/rational/test_rat.rb +++ b/test/-ext-/rational/test_rat.rb @@ -29,4 +29,18 @@ class TestRational < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/rational/test_rat.rb#L29 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 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/