summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--complex.c2
-rw-r--r--rational.c2
-rw-r--r--test/ruby/test_complex.rb8
-rw-r--r--test/ruby/test_rational.rb6
5 files changed, 25 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b190eecf2ea..e8802d7f28b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Wed Jun 17 08:14:01 2009 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * 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@ruby-lang.org>
* lib/webrick/httputils.rb (parse_form_data): escape boundary of
diff --git a/complex.c b/complex.c
index df190d85ac5..0e9a17b7a69 100644
--- a/complex.c
+++ b/complex.c
@@ -747,6 +747,8 @@ nucomp_coerce(VALUE self, VALUE other)
{
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));
diff --git a/rational.c b/rational.c
index b5c93caa609..3fb0b2a043e 100644
--- a/rational.c
+++ b/rational.c
@@ -900,6 +900,8 @@ nurat_coerce(VALUE self, VALUE other)
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",
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 74d6d5dd8a3..39a02044fa7 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -505,6 +505,14 @@ class Complex_Test < Test::Unit::TestCase
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))
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index cb7b58b4171..8cacab9a092 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -704,6 +704,12 @@ class Rational_Test < Test::Unit::TestCase
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))