diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-27 16:45:09 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-02-27 16:45:09 +0000 |
commit | fe2a6f65b1108fc71d269daaf1c7f4d89ebec0cb (patch) | |
tree | 822d5d361d37977ece6a0dc0d85f04c549151d18 | |
parent | 71ee64a5b1e3c0937213a68f883bece12aa24a82 (diff) |
Support two `to_r`-responding args in Rational()
* rational.c (nurat_s_convert): call `to_r` to convert non-Numeric
objects also if argc == 2 in Rational().
* test/ruby/test_rational.rb: add tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62598 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | rational.c | 6 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/rational.c b/rational.c index c350ad32b7..b648b19438 100644 --- a/rational.c +++ b/rational.c @@ -2548,6 +2548,12 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass) return rb_convert_type_with_id(a1, T_RATIONAL, "Rational", idTo_r); } else { + if (!k_numeric_p(a1)) { + a1 = rb_check_convert_type_with_id(a1, T_RATIONAL, "Rational", idTo_r); + } + if (!k_numeric_p(a2)) { + a2 = rb_check_convert_type_with_id(a2, T_RATIONAL, "Rational", idTo_r); + } if ((k_numeric_p(a1) && k_numeric_p(a2)) && (!f_integer_p(a1) || !f_integer_p(a2))) return f_div(a1, a2); diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index f8da6da7d6..4cdcb419cb 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -120,7 +120,32 @@ class Rational_Test < Test::Unit::TestCase assert_raise_with_message(ArgumentError, /\u{221a 2668}/) { Rational("\u{221a 2668}") } + assert_raise(TypeError){Rational(Object.new)} + assert_raise(TypeError){Rational(Object.new, Object.new)} + assert_raise(TypeError){Rational(1, Object.new)} + + o = Object.new + def o.to_r; 1/42r; end + assert_equal(1/42r, Rational(o)) + assert_equal(1/84r, Rational(o, 2)) + assert_equal(42, Rational(1, o)) + assert_equal(1, Rational(o, o)) + + o = Object.new + def o.to_r; nil; end + assert_raise(TypeError) { Rational(o) } + assert_raise(TypeError) { Rational(o, 2) } + assert_raise(TypeError) { Rational(1, o) } + assert_raise(TypeError) { Rational(o, o) } + + o = Object.new + def o.to_r; raise; end + assert_raise(RuntimeError) { Rational(o) } + assert_raise(RuntimeError) { Rational(o, 2) } + assert_raise(RuntimeError) { Rational(1, o) } + assert_raise(RuntimeError) { Rational(o, o) } + assert_raise(ArgumentError){Rational()} assert_raise(ArgumentError){Rational(1,2,3)} |