ruby-changes:59724
From: Kenta <ko1@a...>
Date: Fri, 17 Jan 2020 12:21:19 +0900 (JST)
Subject: [ruby-changes:59724] 5275d8bf4c (master): rb_rational_raw: convert num and den by to_int
https://git.ruby-lang.org/ruby.git/commit/?id=5275d8bf4c From 5275d8bf4c43db9f057d24a26cf33ecd69f8b345 Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Fri, 17 Jan 2020 10:47:20 +0900 Subject: rb_rational_raw: convert num and den by to_int diff --git a/rational.c b/rational.c index 0261c69..1cd4ecc 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 (! RB_INTEGER_TYPE_P(x)) + x = rb_to_int(x); + if (! RB_INTEGER_TYPE_P(y)) + y = rb_to_int(y); if (INT_NEGATIVE_P(y)) { x = rb_int_uminus(x); y = rb_int_uminus(y); diff --git a/test/-ext-/rational/test_rat.rb b/test/-ext-/rational/test_rat.rb index dbba00c..7683483 100644 --- a/test/-ext-/rational/test_rat.rb +++ b/test/-ext-/rational/test_rat.rb @@ -42,5 +42,29 @@ class TestRational < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/rational/test_rat.rb#L42 rat = Rational.raw(1, -2) assert_equal(-1, rat.numerator) assert_equal(2, rat.denominator) + + assert_equal(1/2r, Rational.raw(1.0, 2.0)) + + assert_raise(TypeError) { Rational.raw("1", 2) } + assert_raise(TypeError) { Rational.raw(1, "2") } + + class << (o = Object.new) + def to_i; 42; end + end + + assert_raise(TypeError) { Rational.raw(o, 2) } + assert_raise(TypeError) { Rational.raw(1, o) } + + class << (o = Object.new) + def to_int; 42; end + end + + rat = Rational.raw(o, 2) + assert_equal(42, rat.numerator) + assert_equal(2, rat.denominator) + + rat = Rational.raw(2, o) + assert_equal(2, rat.numerator) + assert_equal(42, rat.denominator) end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/