ruby-changes:12065
From: tadf <ko1@a...>
Date: Thu, 18 Jun 2009 22:42:00 +0900 (JST)
Subject: [ruby-changes:12065] Ruby:r23735 (trunk): * rational.c (nurat_s_convert): calls to_r when the given argument
tadf 2009-06-18 22:41:44 +0900 (Thu, 18 Jun 2009) New Revision: 23735 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23735 Log: * 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. Modified files: trunk/ChangeLog trunk/complex.c trunk/rational.c trunk/test/ruby/test_complex.rb trunk/test/ruby/test_rational.rb Index: complex.c =================================================================== --- complex.c (revision 23734) +++ complex.c (revision 23735) @@ -1248,6 +1248,9 @@ 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 @@ 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)) && Index: ChangeLog =================================================================== --- ChangeLog (revision 23734) +++ ChangeLog (revision 23735) @@ -1,3 +1,13 @@ +Thu Jun 18 22:31:38 2009 Tadayoshi Funaba <tadf@d...> + + * 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@d...> * numeric.c (num_numerator, num_denominator): use Index: test/ruby/test_complex.rb =================================================================== --- test/ruby/test_complex.rb (revision 23734) +++ test/ruby/test_complex.rb (revision 23735) @@ -126,8 +126,8 @@ 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)} Index: test/ruby/test_rational.rb =================================================================== --- test/ruby/test_rational.rb (revision 23734) +++ test/ruby/test_rational.rb (revision 23735) @@ -124,9 +124,9 @@ 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)} Index: rational.c =================================================================== --- rational.c (revision 23734) +++ rational.c (revision 23735) @@ -1410,6 +1410,9 @@ 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 @@ } 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)) && -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/