From: Marc-Andre Lafortune Date: 2011-08-10T10:41:03+09:00 Subject: [ruby-core:38885] [Ruby 1.9 - Bug #5178][Open] Complex#rationalize should rationalize Issue #5178 has been reported by Marc-Andre Lafortune. ---------------------------------------- Bug #5178: Complex#rationalize should rationalize http://redmine.ruby-lang.org/issues/5178 Author: Marc-Andre Lafortune Status: Open Priority: Normal Assignee: Category: core Target version: ruby -v: r32354 Complex#rationalize currently calls `to_r` on the real part instead of `rationalize`: f = 1/3.0 c = Complex(f) c.to_r == f.to_r # => true c.rationalize == f.rationalize # => false Should I not commit this to 1.9.3 too? diff --git a/complex.c b/complex.c index 78f0902..1b76074 100644 --- a/complex.c +++ b/complex.c @@ -1335,7 +1335,8 @@ nucomp_to_f(VALUE self) * call-seq: * cmp.to_r -> rational * - * Returns the value as a rational if possible. + * If the imaginary part is exactly 0, returns the real part as a Rational, + * otherwise a RangeError is raised. */ static VALUE nucomp_to_r(VALUE self) @@ -1354,14 +1355,22 @@ nucomp_to_r(VALUE self) * call-seq: * cmp.rationalize([eps]) -> rational * - * Returns the value as a rational if possible. An optional argument - * eps is always ignored. + * If the imaginary part is exactly 0, returns the real part as a Rational, + * otherwise a RangeError is raised. */ static VALUE nucomp_rationalize(int argc, VALUE *argv, VALUE self) { + get_dat1(self); + rb_scan_args(argc, argv, "01", NULL); - return nucomp_to_r(self); + + if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) { + VALUE s = f_to_s(self); + rb_raise(rb_eRangeError, "can't convert %s into Rational", + StringValuePtr(s)); + } + return rb_funcall(dat->real, rb_intern("rationalize"), argc, argv); } -- http://redmine.ruby-lang.org