diff options
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -1700,6 +1700,38 @@ int_int_p(VALUE num) /* * call-seq: + * int.odd? -> true or false + * + * Returns <code>true</code> if <i>int</i> 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 <code>true</code> if <i>int</i> 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 * @@ -2799,6 +2831,38 @@ fix_zero_p(VALUE num) return Qfalse; } +/* + * call-seq: + * fix.odd? -> true or false + * + * Returns <code>true</code> if <i>fix</i> 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 <code>true</code> if <i>fix</i> is an even number. + */ + +static VALUE +fix_even_p(VALUE num) +{ + if (num & 2) { + return Qfalse; + } + return Qtrue; +} + void Init_Numeric(void) { @@ -2853,6 +2917,8 @@ Init_Numeric(void) 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); @@ -2910,6 +2976,8 @@ Init_Numeric(void) 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_cInteger, "odd?", fix_odd_p, 0); + rb_define_method(rb_cInteger, "even?", fix_even_p, 0); rb_cFloat = rb_define_class("Float", rb_cNumeric); |