diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-19 09:28:12 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-19 09:28:12 +0000 |
commit | 81f65a9e054ddcdf2d1a8507e516672dd8c7324e (patch) | |
tree | 6b6ce7ab2c9ab3be6004b00ddd8e78c6f7c192b2 /numeric.c | |
parent | bc343b851d04f68ef7554a2cca0bad63a32c2b7d (diff) |
* 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
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 41 |
1 files changed, 28 insertions, 13 deletions
@@ -3416,9 +3416,29 @@ fix_equal(VALUE x, VALUE y) } } +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) */ 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); - } - else if (RB_TYPE_P(y, T_BIGNUM)) { - return rb_big_cmp(rb_int2big(FIX2LONG(x)), y); + if (FIXNUM_P(x)) { + return fix_cmp(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) 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); |