From 81f65a9e054ddcdf2d1a8507e516672dd8c7324e Mon Sep 17 00:00:00 2001 From: mrkn Date: Sat, 19 Mar 2016 09:28:12 +0000 Subject: [PATCH] * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54190 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 13 ++++++++ bignum.c | 1 - numeric.c | 43 ++++++++++++++++++--------- test/-ext-/integer/test_my_integer.rb | 22 ++++++++++++++ 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index bf4075e5d4..60a3832738 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Sat Mar 19 18:21:00 2016 Kenta Murata + + * 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 * iseq.c (rb_iseq_compile_with_option): make the parser in mild diff --git a/bignum.c b/bignum.c index 2c241d1c56..450385ce3e 100644 --- a/bignum.c +++ b/bignum.c @@ -7011,7 +7011,6 @@ Init_Bignum(void) 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); diff --git a/numeric.c b/numeric.c index 99ad0882b8..8b566a214c 100644 --- a/numeric.c +++ b/numeric.c @@ -3416,18 +3416,6 @@ fix_equal(VALUE x, VALUE y) } } -/* - * call-seq: - * fix <=> 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+. - * - * This is the basis for the tests in the Comparable module. - * - * +nil+ is returned if the two values are incomparable. - */ - static VALUE fix_cmp(VALUE x, VALUE y) { @@ -3440,11 +3428,38 @@ fix_cmp(VALUE x, VALUE y) return rb_big_cmp(rb_int2big(FIX2LONG(x)), y); } else if (RB_TYPE_P(y, T_FLOAT)) { - return rb_integer_float_cmp(x, y); + 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: + * 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+. + * + * This is the basis for the tests in the Comparable module. + * + * +nil+ is returned if the two values are incomparable. + */ + +static VALUE +int_cmp(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_cmp(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_cmp(x, y); + } + else { + rb_raise(rb_eNotImpError, "need to define `<=>' in %s", rb_obj_classname(x)); + } } /* @@ -4228,6 +4243,7 @@ Init_Numeric(void) 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) 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); diff --git a/test/-ext-/integer/test_my_integer.rb b/test/-ext-/integer/test_my_integer.rb index bc4757f463..e6d8dd1a07 100644 --- a/test/-ext-/integer/test_my_integer.rb +++ b/test/-ext-/integer/test_my_integer.rb @@ -23,4 +23,26 @@ class TestIntegerExt < Test::Unit::TestCase 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