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

ruby-changes:29057

From: mrkn <ko1@a...>
Date: Thu, 6 Jun 2013 22:31:35 +0900 (JST)
Subject: [ruby-changes:29057] mrkn:r41109 (trunk): * numeric.c (num_quo): Use to_r method to convert the receiver to

mrkn	2013-06-06 22:31:22 +0900 (Thu, 06 Jun 2013)

  New Revision: 41109

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

  Log:
    * numeric.c (num_quo): Use to_r method to convert the receiver to
      rational.  [ruby-core:41575] [Bug #5736]
    
    * test/ruby/test_numeric.rb: add a test for the above change.

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c
    trunk/test/ruby/test_numeric.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41108)
+++ ChangeLog	(revision 41109)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Jun  6 22:24:00 2013  Kenta Murata  <mrkn@m...>
+
+	* numeric.c (num_quo): Use to_r method to convert the receiver to
+	  rational.  [ruby-core:41575] [Bug #5736]
+
+	* test/ruby/test_numeric.rb: add a test for the above change.
+
 Thu Jun  6 20:40:17 2013  Tanaka Akira  <akr@f...>
 
 	* configure.in: Invoke RUBY_REPLACE_TYPE for size_t.
Index: numeric.c
===================================================================
--- numeric.c	(revision 41108)
+++ numeric.c	(revision 41109)
@@ -101,7 +101,7 @@ static VALUE fix_uminus(VALUE num); https://github.com/ruby/ruby/blob/trunk/numeric.c#L101
 static VALUE fix_mul(VALUE x, VALUE y);
 static VALUE int_pow(long x, unsigned long y);
 
-static ID id_coerce, id_to_i, id_eq, id_div;
+static ID id_coerce, id_to_i, id_to_r, id_eq, id_div;
 
 VALUE rb_cNumeric;
 VALUE rb_cFloat;
@@ -398,7 +398,8 @@ num_quo(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L398
         return num_fdiv(x, y);
     }
 
-    return rb_funcall(rb_rational_raw1(x), '/', 1, y);
+    x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r");
+    return rb_funcall(x, '/', 1, y);
 }
 
 
@@ -3774,6 +3775,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3775
 #endif
     id_coerce = rb_intern("coerce");
     id_to_i = rb_intern("to_i");
+    id_to_r = rb_intern("to_r");
     id_eq = rb_intern("==");
     id_div = rb_intern("div");
 
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 41108)
+++ test/ruby/test_numeric.rb	(revision 41109)
@@ -54,7 +54,20 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L54
   end
 
   def test_quo
-    assert_raise(ArgumentError) {DummyNumeric.new.quo(1)}
+    assert_raise(TypeError) {DummyNumeric.new.quo(1)}
+  end
+
+  def test_quo_ruby_core_41575
+    x = DummyNumeric.new
+    rat = 84.quo(1)
+    DummyNumeric.class_eval do
+      define_method(:to_r) { rat }
+    end
+    assert_equal(2.quo(1), x.quo(42), '[ruby-core:41575]')
+  ensure
+    DummyNumeric.class_eval do
+      remove_method :to_r
+    end
   end
 
   def test_divmod

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

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