mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* rational.c (nurat_s_convert): calls to_r when the given argument
is non-integer. * rational.c (nurat_s_convert): raises TypeError when the given argument is nil. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23735 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
430eb945ce
commit
67f93ddc21
5 changed files with 25 additions and 6 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
|||
Thu Jun 18 22:31:38 2009 Tadayoshi Funaba <tadf@dotrb.org>
|
||||
|
||||
* rational.c (nurat_s_convert): calls to_r when the given argument
|
||||
is non-integer.
|
||||
|
||||
* rational.c (nurat_s_convert): raises TypeError when the given
|
||||
argument is nil.
|
||||
|
||||
* complex.c (nucomp_s_convert): ditto.
|
||||
|
||||
Thu Jun 18 20:32:11 2009 Tadayoshi Funaba <tadf@dotrb.org>
|
||||
|
||||
* numeric.c (num_numerator, num_denominator): use
|
||||
|
|
|
@ -1248,6 +1248,9 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
|
|||
|
||||
rb_scan_args(argc, argv, "11", &a1, &a2);
|
||||
|
||||
if (NIL_P(a1) || (argc == 2 && NIL_P(a2)))
|
||||
rb_raise(rb_eTypeError, "can't convert nil into Complex");
|
||||
|
||||
backref = rb_backref_get();
|
||||
rb_match_busy(backref);
|
||||
|
||||
|
@ -1302,6 +1305,9 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
|
|||
if (argc == 1) {
|
||||
if (k_numeric_p(a1) && !f_real_p(a1))
|
||||
return a1;
|
||||
/* expect raise exception for consistency */
|
||||
if (!k_numeric_p(a1))
|
||||
return rb_convert_type(a1, T_COMPLEX, "Complex", "to_c");
|
||||
}
|
||||
else {
|
||||
if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
|
||||
|
|
|
@ -1410,6 +1410,9 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass)
|
|||
|
||||
rb_scan_args(argc, argv, "11", &a1, &a2);
|
||||
|
||||
if (NIL_P(a1) || (argc == 2 && NIL_P(a2)))
|
||||
rb_raise(rb_eTypeError, "can't convert nil into Rational");
|
||||
|
||||
switch (TYPE(a1)) {
|
||||
case T_COMPLEX:
|
||||
if (k_exact_p(RCOMPLEX(a1)->imag) && f_zero_p(RCOMPLEX(a1)->imag))
|
||||
|
@ -1458,8 +1461,8 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass)
|
|||
}
|
||||
|
||||
if (argc == 1) {
|
||||
if (k_numeric_p(a1) && !f_integer_p(a1))
|
||||
return a1;
|
||||
if (!(k_numeric_p(a1) && k_integer_p(a1)))
|
||||
return rb_convert_type(a1, T_RATIONAL, "Rational", "to_r");
|
||||
}
|
||||
else {
|
||||
if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
|
||||
|
|
|
@ -126,8 +126,8 @@ class Complex_Test < Test::Unit::TestCase
|
|||
if @rational && !@keiju
|
||||
assert_equal(Complex(1,1),Complex('3/3','3/3'))
|
||||
end
|
||||
assert_raise(ArgumentError){Complex(nil)}
|
||||
assert_raise(ArgumentError){Complex(Object.new)}
|
||||
assert_raise(TypeError){Complex(nil)}
|
||||
assert_raise(TypeError){Complex(Object.new)}
|
||||
assert_raise(ArgumentError){Complex()}
|
||||
assert_raise(ArgumentError){Complex(1,2,3)}
|
||||
|
||||
|
|
|
@ -124,9 +124,9 @@ class Rational_Test < Test::Unit::TestCase
|
|||
assert_equal(Rational(3),Rational('3'))
|
||||
assert_equal(Rational(1),Rational('3.0','3.0'))
|
||||
assert_equal(Rational(1),Rational('3/3','3/3'))
|
||||
assert_raise(ArgumentError){Rational(nil)}
|
||||
assert_raise(TypeError){Rational(nil)}
|
||||
assert_raise(ArgumentError){Rational('')}
|
||||
assert_raise(ArgumentError){Rational(Object.new)}
|
||||
assert_raise(TypeError){Rational(Object.new)}
|
||||
assert_raise(ArgumentError){Rational()}
|
||||
assert_raise(ArgumentError){Rational(1,2,3)}
|
||||
|
||||
|
|
Loading…
Reference in a new issue