summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-10-21 07:42:50 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2013-10-21 07:42:50 +0900
commit3ae0616710b10b091baa130f85a56b5ef2c66633 (patch)
treebac5db924b04c17d12cc28f13559d8da4c6ad243
parent9cb58a2252d25f81346ae3d4eefb1775d9ed548b (diff)
downloadmruby-3ae0616710b10b091baa130f85a56b5ef2c66633.tar.gz
mruby-3ae0616710b10b091baa130f85a56b5ef2c66633.zip
implement Integer#succ in Ruby
-rw-r--r--mrblib/numeric.rb10
-rw-r--r--src/numeric.c42
2 files changed, 10 insertions, 42 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb
index daa35c5d6..a5aa3e451 100644
--- a/mrblib/numeric.rb
+++ b/mrblib/numeric.rb
@@ -35,6 +35,16 @@ class Integer
end
##
+ # Returns self + 1
+ #
+ # ISO 15.2.8.3.19
+ def next
+ self + 1
+ end
+ # ISO 15.2.8.3.21
+ alias succ next
+
+ ##
# Calls the given block +self+ times.
#
# ISO 15.2.8.3.22
diff --git a/src/numeric.c b/src/numeric.c
index 1cb47a8f2..f8779c0cd 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -695,46 +695,6 @@ int_to_i(mrb_state *mrb, mrb_value num)
return num;
}
-/* 15.2.8.3.21 */
-/*
- * call-seq:
- * fixnum.next -> integer
- * fixnum.succ -> integer
- *
- * Returns the <code>Integer</code> equal to <i>int</i> + 1.
- *
- * 1.next #=> 2
- * (-1).next #=> 0
- */
-
-static mrb_value
-fix_succ(mrb_state *mrb, mrb_value num)
-{
- mrb_int x = mrb_fixnum(num);
-
- if (x == MRB_INT_MAX) /* fixnum overflow */
- return mrb_float_value(mrb, (double)x + 1.0);
- return mrb_fixnum_value(x+1);
-}
-
-/* 15.2.8.3.19 */
-/*
- * call-seq:
- * int.next -> integer
- * int.succ -> integer
- *
- * Returns the <code>Integer</code> equal to <i>int</i> + 1.
- *
- * 1.next #=> 2
- * (-1).next #=> 0
- */
-static mrb_value
-int_succ(mrb_state *mrb, mrb_value num)
-{
- if (mrb_fixnum_p(num)) return fix_succ(mrb, num);
- return mrb_funcall(mrb, num, "+", 1, mrb_fixnum_value(1));
-}
-
#define SQRT_INT_MAX ((mrb_int)1<<((sizeof(mrb_int)*CHAR_BIT-1)/2))
/*tests if N*N would overflow*/
#define FIT_SQRT_INT(n) (((n)<SQRT_INT_MAX)&&((n)>=-SQRT_INT_MAX))
@@ -1394,8 +1354,6 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method(mrb, fixnum, ">>", fix_rshift, MRB_ARGS_REQ(1)); /* 15.2.8.3.13 */
mrb_define_method(mrb, fixnum, "eql?", num_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */
mrb_define_method(mrb, fixnum, "hash", flo_hash, MRB_ARGS_NONE()); /* 15.2.8.3.18 */
- mrb_define_method(mrb, fixnum, "next", int_succ, MRB_ARGS_NONE()); /* 15.2.8.3.19 */
- mrb_define_method(mrb, fixnum, "succ", fix_succ, MRB_ARGS_NONE()); /* 15.2.8.3.21 */
mrb_define_method(mrb, fixnum, "to_f", fix_to_f, MRB_ARGS_NONE()); /* 15.2.8.3.23 */
mrb_define_method(mrb, fixnum, "to_s", fix_to_s, MRB_ARGS_NONE()); /* 15.2.8.3.25 */
mrb_define_method(mrb, fixnum, "inspect", fix_to_s, MRB_ARGS_NONE());