Integer#odd?, Integer#even?, Integer#pred for 1.8

e$B$U$He(B 1.9 e$B$Ne(B test_integer.rb e$B$re(B 1.8
e$B$GF0$+$=$&$H$7$?$i!"%a%=%Ce(B
e$B%I$,B-$j$J$+$C$?$N$Ge(B Integer#odd?, Integer#even?,
Integer#pred e$B$re(B backport
e$B$7$F$7$^$C$?$s$G$9$,!"$I$&$G$9$+$M!#e(B

Index: numeric.c

— numeric.c (e$B%j%S%8%g%se(B 13948)
+++ numeric.c (e$B:n6H%3%T!<e(B)
@@ -1786,6 +1786,39 @@

/*

  • call-seq:
    • int.odd? -> true or false
      
    • Returns true if int is an odd number.
  • */

+static VALUE
+int_odd_p(VALUE num)
+{

  • if (rb_funcall(num, ‘%’, 1, INT2FIX(2)) != INT2FIX(0)) {
  •    return Qtrue;
    
  • }
  • return Qfalse;
    +}

+/*

    • call-seq:
    • int.even? -> true or false
      
    • Returns true if int is an even number.
  • */

+static VALUE
+int_even_p(VALUE num)
+{

  • if (rb_funcall(num, ‘%’, 1, INT2FIX(2)) == INT2FIX(0)) {
  •    return Qtrue;
    
  • }
  • return Qfalse;
    +}

+/*

    • call-seq:
    • int.next    => integer
      
    • int.succ    => integer
      

@@ -1808,6 +1841,26 @@

/*

  • call-seq:
    • int.pred    => integer
      
    • Returns the Integer equal to int - 1.
    • 1.pred      #=> 0
      
    • (-1).pred   #=> -2
      
  • */

+static VALUE
+int_pred(VALUE num)
+{

  • if (FIXNUM_P(num)) {
  •    long i = FIX2LONG(num) - 1;
    
  •    return LONG2NUM(i);
    
  • }
  • return rb_funcall(num, ‘-’, 1, INT2FIX(1));
    +}

+/*

    • call-seq:
    • int.chr    => string
      
    • Returns a string containing the ASCII character represented by the
      @@ -2262,15 +2315,6 @@
      return LONG2NUM(z);
      }

-static VALUE
-int_even_p(VALUE num)
-{

  • if (rb_funcall(num, ‘%’, 1, INT2FIX(2)) == INT2FIX(0)) {
  • return Qtrue;
  • }
  • return Qfalse;
    -}

/*

  • call-seq:
  • fix ** other => Numeric
    @@ -2907,6 +2951,38 @@
    return Qfalse;
    }

+/*

    • call-seq:
    • fix.odd? -> true or false
      
    • Returns true if fix is an odd number.
  • */

+static VALUE
+fix_odd_p(VALUE num)
+{

  • if (num & 2) {
  •    return Qtrue;
    
  • }
  • return Qfalse;
    +}

+/*

    • call-seq:
    • fix.even? -> true or false
      
    • Returns true if fix is an even number.
  • */

+static VALUE
+fix_even_p(VALUE num)
+{

  • if (num & 2) {
  •    return Qfalse;
    
  • }
  • return Qtrue;
    +}

void
Init_Numeric()
{
@@ -2961,12 +3037,15 @@
rb_undef_method(CLASS_OF(rb_cInteger), “new”);

 rb_define_method(rb_cInteger, "integer?", int_int_p, 0);
  • rb_define_method(rb_cInteger, “odd?”, int_odd_p, 0);

  • rb_define_method(rb_cInteger, “even?”, int_even_p, 0);
    rb_define_method(rb_cInteger, “upto”, int_upto, 1);
    rb_define_method(rb_cInteger, “downto”, int_downto, 1);
    rb_define_method(rb_cInteger, “times”, int_dotimes, 0);
    rb_include_module(rb_cInteger, rb_mPrecision);
    rb_define_method(rb_cInteger, “succ”, int_succ, 0);
    rb_define_method(rb_cInteger, “next”, int_succ, 0);

  • rb_define_method(rb_cInteger, “pred”, int_pred, 0);
    rb_define_method(rb_cInteger, “chr”, int_chr, 0);
    rb_define_method(rb_cInteger, “ord”, int_ord, 0);
    rb_define_method(rb_cInteger, “to_i”, int_to_i, 0);
    @@ -3020,6 +3099,8 @@
    rb_define_method(rb_cFixnum, “to_f”, fix_to_f, 0);
    rb_define_method(rb_cFixnum, “size”, fix_size, 0);
    rb_define_method(rb_cFixnum, “zero?”, fix_zero_p, 0);

  • rb_define_method(rb_cFixnum, “odd?”, fix_odd_p, 0);

  • rb_define_method(rb_cFixnum, “even?”, fix_even_p, 0);

    rb_cFloat = rb_define_class(“Float”, rb_cNumeric);

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32303] Integer#odd?, Integer#even?,
Integer#pred for 1.8”
on Fri, 16 Nov 2007 21:10:48 +0900, Tanaka A. [email protected]
writes:

|e$B$U$He(B 1.9 e$B$Ne(B test_integer.rb e$B$re(B 1.8 e$B$GF0$+$=$&$H$7$?$i!“%a%=%Ce(B
|e$B%I$,B-$j$J$+$C$?$N$Ge(B Integer#odd?, Integer#even?,
|Integer#pred e$B$re(B backport e$B$7$F$7$^$C$?$s$G$9$,!”$I$&$G$9$+$M!#e(B

e$B$$$$$s$8$c$J$$$G$7$g$&$+!#e(B