summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:13:19 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-06 17:13:19 +0000
commitcef054d5b2d29422b2160783aea6d0581c701483 (patch)
tree12a0e7ebc3a2ea1196bfa6b0c7d3a38e69f4875d /enum.c
parentfe9386cdcbfce8c631a333add2b2cab8366ee6dc (diff)
* enum.c (enum_cycle): Support for Enumerable#cycle.size
[Feature #6636] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37510 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/enum.c b/enum.c
index 75b18454c9..e9c925611d 100644
--- a/enum.c
+++ b/enum.c
@@ -2235,6 +2235,24 @@ cycle_i(VALUE i, VALUE ary, int argc, VALUE *argv)
return Qnil;
}
+static VALUE
+enum_cycle_size(VALUE self, VALUE args)
+{
+ long mul;
+ VALUE n = Qnil;
+ VALUE size = enum_size(self, args);
+
+ if (size == Qnil) return Qnil;
+
+ if (args && (RARRAY_LEN(args) > 0)) {
+ n = RARRAY_PTR(args)[0];
+ }
+ if (n == Qnil) return DBL2NUM(INFINITY);
+ mul = NUM2LONG(n);
+ if (mul <= 0) return INT2FIX(0);
+ return rb_funcall(size, '*', 1, LONG2FIX(mul));
+}
+
/*
* call-seq:
* enum.cycle(n=nil) { |obj| block } -> nil
@@ -2265,7 +2283,7 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
rb_scan_args(argc, argv, "01", &nv);
- RETURN_ENUMERATOR(obj, argc, argv);
+ RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size);
if (NIL_P(nv)) {
n = -1;
}