aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-12 15:07:53 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-12 15:07:53 +0000
commitc7260f9d7cab69fc1ce636795a2097a4ab15f138 (patch)
tree120360ecc321788abde92edfaeb1ecd2d2e6c4d1
parentbd03f7f9a827730f7b9653507b123ecec270e04b (diff)
downloadruby-c7260f9d7cab69fc1ce636795a2097a4ab15f138.tar.gz
rational.c: define Rational#{negative?,positive?}
* rational.c (nurat_{negative,positive}_p): define Rational#negative? and Rational#positive?, respectively. Author: Tadashi Saito <tad.a.digger@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--rational.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/rational.c b/rational.c
index 4545c2c3d5..4e0b9ed053 100644
--- a/rational.c
+++ b/rational.c
@@ -1204,6 +1204,32 @@ nurat_true(VALUE self)
}
#endif
+/*
+ * call-seq:
+ * rat.positive? -> true or false
+ *
+ * Returns +true+ if +rat+ is greater than 0.
+ */
+static VALUE
+nurat_positive_p(VALUE self)
+{
+ get_dat1(self);
+ return f_boolcast(INT_POSITIVE_P(dat->num));
+}
+
+/*
+ * call-seq:
+ * rat.negative? -> true or false
+ *
+ * Returns +true+ if +rat+ is less than 0.
+ */
+static VALUE
+nurat_negative_p(VALUE self)
+{
+ get_dat1(self);
+ return f_boolcast(INT_NEGATIVE_P(dat->num));
+}
+
static VALUE
nurat_floor(VALUE self)
{
@@ -2608,6 +2634,8 @@ Init_Rational(void)
rb_define_method(rb_cRational, "rational?", nurat_true, 0);
rb_define_method(rb_cRational, "exact?", nurat_true, 0);
#endif
+ rb_define_method(rb_cRational, "positive?", nurat_positive_p, 0);
+ rb_define_method(rb_cRational, "negative?", nurat_negative_p, 0);
rb_define_method(rb_cRational, "floor", nurat_floor_n, -1);
rb_define_method(rb_cRational, "ceil", nurat_ceil_n, -1);