diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-09-21 06:09:26 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-09-21 06:09:26 +0000 |
commit | 255702be8ca169429867294d4a1fd818b53af928 (patch) | |
tree | 6c41ad03aac727381190fbb0607b97e08938e2e6 | |
parent | 4e37427ee5ecabda3e59ffd3ad1a6c85d4d9327b (diff) | |
download | ruby-255702be8ca169429867294d4a1fd818b53af928.tar.gz |
* numeric.c (int_odd_p): a new method to check even or odd.
[RCR#337]
* numeric.c (int_even_p): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10982 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | numeric.c | 68 |
2 files changed, 75 insertions, 0 deletions
@@ -1,3 +1,10 @@ +Thu Sep 21 15:06:24 2006 Yukihiro Matsumoto <matz@ruby-lang.org> + + * numeric.c (int_odd_p): a new method to check even or odd. + [RCR#337] + + * numeric.c (int_even_p): ditto. + Thu Sep 21 13:55:07 2006 Yukihiro Matsumoto <matz@ruby-lang.org> * ext/etc/etc.c (etc_getpwuid): uid integer should be wraped in @@ -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); |