mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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
This commit is contained in:
parent
71ee64a5b1
commit
fe2a6f65b1
2 changed files with 31 additions and 0 deletions
|
@ -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);
|
||||
|
|
|
@ -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)}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue