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

ruby-changes:42116

From: mrkn <ko1@a...>
Date: Sat, 19 Mar 2016 18:28:20 +0900 (JST)
Subject: [ruby-changes:42116] mrkn:r54190 (trunk): * bignum.c (Bignum#<=>): remove it because they are unified with

mrkn	2016-03-19 18:28:12 +0900 (Sat, 19 Mar 2016)

  New Revision: 54190

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54190

  Log:
    * bignum.c (Bignum#<=>): remove it because they are unified with
      Integer#<=>.
    
    * numeric.c (Integer#<=>, Fixnum#<=>): move <=> method from Fixnum to
      Integer.
    
    * numeric.c (int_cmp): add this method for Integer#<=>.
    
    * test/-ext-/integer/test_my_integer.rb (test_my_integer_cmp): add a
      test to examine Integer#<=> for unknown subclasses.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/numeric.c
    trunk/test/-ext-/integer/test_my_integer.rb
Index: bignum.c
===================================================================
--- bignum.c	(revision 54189)
+++ bignum.c	(revision 54190)
@@ -7011,7 +7011,6 @@ Init_Bignum(void) https://github.com/ruby/ruby/blob/trunk/bignum.c#L7011
     rb_define_method(rb_cBignum, ">>", rb_big_rshift, 1);
     rb_define_method(rb_cBignum, "[]", rb_big_aref, 1);
 
-    rb_define_method(rb_cBignum, "<=>", rb_big_cmp, 1);
     rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
     rb_define_method(rb_cBignum, ">", big_gt, 1);
     rb_define_method(rb_cBignum, ">=", big_ge, 1);
Index: test/-ext-/integer/test_my_integer.rb
===================================================================
--- test/-ext-/integer/test_my_integer.rb	(revision 54189)
+++ test/-ext-/integer/test_my_integer.rb	(revision 54190)
@@ -23,4 +23,26 @@ class TestIntegerExt < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/-ext-/integer/test_my_integer.rb#L23
       end
     end
   end
+
+  def test_my_integer_cmp
+    assert_raise(NotImplementedError) do
+      Bug::Integer::MyInteger.new <=> 0
+    end
+
+    begin
+      Bug::Integer::MyInteger.class_eval do
+        def <=>(other)
+          0
+        end
+      end
+
+      assert_nothing_raised do
+        Bug::Integer::MyInteger.new <=> 0
+      end
+    ensure
+      Bug::Integer::MyInteger.class_eval do
+        remove_method :<=>
+      end
+    end
+  end
 end
Index: numeric.c
===================================================================
--- numeric.c	(revision 54189)
+++ numeric.c	(revision 54190)
@@ -3416,9 +3416,29 @@ fix_equal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3416
     }
 }
 
+static VALUE
+fix_cmp(VALUE x, VALUE y)
+{
+    if (x == y) return INT2FIX(0);
+    if (FIXNUM_P(y)) {
+	if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
+	return INT2FIX(-1);
+    }
+    else if (RB_TYPE_P(y, T_BIGNUM)) {
+	return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
+    }
+    else if (RB_TYPE_P(y, T_FLOAT)) {
+	return rb_integer_float_cmp(x, y);
+    }
+    else {
+	return rb_num_coerce_cmp(x, y, id_cmp);
+    }
+    return rb_num_coerce_cmp(x, y, id_cmp);
+}
+
 /*
  *  call-seq:
- *     fix <=> numeric  ->  -1, 0, +1 or nil
+ *     int <=> numeric  ->  -1, 0, +1 or nil
  *
  *  Comparison---Returns +-1+, +0+, ++1+ or +nil+ depending on whether +fix+ is
  *  less than, equal to, or greater than +numeric+.
@@ -3429,21 +3449,16 @@ fix_equal(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3449
  */
 
 static VALUE
-fix_cmp(VALUE x, VALUE y)
+int_cmp(VALUE x, VALUE y)
 {
-    if (x == y) return INT2FIX(0);
-    if (FIXNUM_P(y)) {
-	if (FIX2LONG(x) > FIX2LONG(y)) return INT2FIX(1);
-	return INT2FIX(-1);
+    if (FIXNUM_P(x)) {
+	return fix_cmp(x, y);
     }
-    else if (RB_TYPE_P(y, T_BIGNUM)) {
-	return rb_big_cmp(rb_int2big(FIX2LONG(x)), y);
-    }
-    else if (RB_TYPE_P(y, T_FLOAT)) {
-        return rb_integer_float_cmp(x, y);
+    else if (RB_TYPE_P(x, T_BIGNUM)) {
+	return rb_big_cmp(x, y);
     }
     else {
-	return rb_num_coerce_cmp(x, y, id_cmp);
+	rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x));
     }
 }
 
@@ -4228,6 +4243,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4243
     rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
     rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
     rb_define_method(rb_cInteger, "round", int_round, -1);
+    rb_define_method(rb_cInteger, "<=>", int_cmp, 1);
 
     rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
 
@@ -4248,7 +4264,6 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4264
 
     rb_define_method(rb_cFixnum, "==", fix_equal, 1);
     rb_define_method(rb_cFixnum, "===", fix_equal, 1);
-    rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
     rb_define_method(rb_cFixnum, ">",  fix_gt, 1);
     rb_define_method(rb_cFixnum, ">=", fix_ge, 1);
     rb_define_method(rb_cFixnum, "<",  fix_lt, 1);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54189)
+++ ChangeLog	(revision 54190)
@@ -1,3 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Mar 19 18:21:00 2016  Kenta Murata  <mrkn@m...>
+
+	* bignum.c (Bignum#<=>): remove it because they are unified with
+	  Integer#<=>.
+
+	* numeric.c (Integer#<=>, Fixnum#<=>): move <=> method from Fixnum to
+	  Integer.
+
+	* numeric.c (int_cmp): add this method for Integer#<=>.
+
+	* test/-ext-/integer/test_my_integer.rb (test_my_integer_cmp): add a
+	  test to examine Integer#<=> for unknown subclasses.
+
 Sat Mar 19 14:46:18 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* iseq.c (rb_iseq_compile_with_option): make the parser in mild

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

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