[前][次][番号順一覧][スレッド一覧]

ruby-changes:12051

From: tadf <ko1@a...>
Date: Wed, 17 Jun 2009 08:17:46 +0900 (JST)
Subject: [ruby-changes:12051] Ruby:r23718 (trunk): * complex.c (nucomp_coerce): accepts Complex instances.

tadf	2009-06-17 08:17:17 +0900 (Wed, 17 Jun 2009)

  New Revision: 23718

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=23718

  Log:
    * complex.c (nucomp_coerce): accepts Complex instances.
    * rational.c (nurat_coerce): accepts Rational
      instances. [ruby-core:23859]

  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 23717)
+++ complex.c	(revision 23718)
@@ -747,6 +747,8 @@
 {
     if (k_numeric_p(other) && f_real_p(other))
 	return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
+    if (TYPE(other) == T_COMPLEX)
+	return rb_assoc_new(other, self);
 
     rb_raise(rb_eTypeError, "%s can't be coerced into %s",
 	     rb_obj_classname(other), rb_obj_classname(self));
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 23717)
+++ ChangeLog	(revision 23718)
@@ -1,3 +1,10 @@
+Wed Jun 17 08:14:01 2009  Tadayoshi Funaba  <tadf@d...>
+
+	* complex.c (nucomp_coerce): accepts Complex instances.
+
+	* rational.c (nurat_coerce): accepts Rational
+	  instances. [ruby-core:23859]
+
 Wed Jun 17 07:36:22 2009  NARUSE, Yui  <naruse@r...>
 
 	* lib/webrick/httputils.rb (parse_form_data): escape boundary of
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 23717)
+++ test/ruby/test_complex.rb	(revision 23718)
@@ -505,6 +505,14 @@
     end
   end
 
+  def test_coerce
+    assert_equal([Complex(2),Complex(1)], Complex(1).coerce(2))
+    assert_equal([Complex(2.2),Complex(1)], Complex(1).coerce(2.2))
+    assert_equal([Complex(Rational(2)),Complex(1)],
+		 Complex(1).coerce(Rational(2)))
+    assert_equal([Complex(2),Complex(1)], Complex(1).coerce(Complex(2)))
+  end
+
   def test_unify
     if @unify
       assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2))
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 23717)
+++ test/ruby/test_rational.rb	(revision 23718)
@@ -704,6 +704,12 @@
     assert_equal(false, Rational(1) == '')
   end
 
+  def test_coerce
+    assert_equal([Rational(2),Rational(1)], Rational(1).coerce(2))
+    assert_equal([Rational(2.2),Rational(1)], Rational(1).coerce(2.2))
+    assert_equal([Rational(2),Rational(1)], Rational(1).coerce(Rational(2)))
+  end
+
   def test_unify
     if @unify
       assert_instance_of(Fixnum, Rational(1,2) + Rational(1,2))
Index: rational.c
===================================================================
--- rational.c	(revision 23717)
+++ rational.c	(revision 23718)
@@ -900,6 +900,8 @@
 	return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
       case T_FLOAT:
 	return rb_assoc_new(other, f_to_f(self));
+      case T_RATIONAL:
+	return rb_assoc_new(other, self);
     }
 
     rb_raise(rb_eTypeError, "%s can't be coerced into %s",

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]