summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-01-30 04:27:04 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-01-30 04:27:04 +0000
commit8418ea8bb86d5e205e3bc11c5d034e112418500e (patch)
tree74abfa5b0a799fe2adc843ef666c503313f0a493 /numeric.c
parentbfc2a070b1c1d877dee7050bb5fa89ec56dabc12 (diff)
* numeric.c (int_pred): add Integer#pred corresponding
Integer#succ. [RCR#5] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11596 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/numeric.c b/numeric.c
index 43fbfe8638..aa5c275606 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1768,6 +1768,26 @@ int_succ(VALUE num)
/*
* call-seq:
+ * int.pred => integer
+ *
+ * Returns the <code>Integer</code> equal to <i>int</i> - 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
@@ -2948,6 +2968,7 @@ Init_Numeric(void)
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, "to_i", int_to_i, 0);
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);