mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rb_rational_raw: convert num and den by to_int
This commit is contained in:
parent
47465ab1cc
commit
5275d8bf4c
2 changed files with 28 additions and 0 deletions
|
@ -1927,6 +1927,10 @@ rb_gcdlcm(VALUE self, VALUE other)
|
||||||
VALUE
|
VALUE
|
||||||
rb_rational_raw(VALUE x, VALUE y)
|
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)) {
|
if (INT_NEGATIVE_P(y)) {
|
||||||
x = rb_int_uminus(x);
|
x = rb_int_uminus(x);
|
||||||
y = rb_int_uminus(y);
|
y = rb_int_uminus(y);
|
||||||
|
|
|
@ -42,5 +42,29 @@ class TestRational < Test::Unit::TestCase
|
||||||
rat = Rational.raw(1, -2)
|
rat = Rational.raw(1, -2)
|
||||||
assert_equal(-1, rat.numerator)
|
assert_equal(-1, rat.numerator)
|
||||||
assert_equal(2, rat.denominator)
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue