summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-06 16:26:17 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-06 16:26:17 +0000
commitb3e977a4c03e435a9a6edd305b03a2769c31df96 (patch)
tree0fd33678b9b4bbb6c86d83f37378d534093433c7 /enum.c
parent5956c7ab3ec70bc798b6843e0fb17e22424d5083 (diff)
* enum.c (enum_cycle): new method to cycle enumerable forever.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12890 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r--enum.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/enum.c b/enum.c
index a93b171c08..3ca42ca4bd 100644
--- a/enum.c
+++ b/enum.c
@@ -1526,6 +1526,44 @@ enum_drop(int argc, VALUE *argv, VALUE obj)
return args[0];
}
+
+static VALUE
+cycle_i(VALUE i, VALUE ary)
+{
+ rb_ary_push(ary, i);
+ rb_yield(i);
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * enum.cycle {|obj| block }
+ *
+ * Calls <i>block</i> for each element of enumerable repeatedly
+ * forever. Enumerable#cycle saves elements in an internal array.
+ *
+ * a = ["a", "b", "c"]
+ * a.each {|x| puts x } # print, a, b, c, a, b, c,.. forever.
+ *
+ */
+
+static VALUE
+enum_cycle(VALUE obj)
+{
+ VALUE ary;
+ long i;
+
+ RETURN_ENUMERATOR(obj, 0, 0);
+ ary = rb_ary_new();
+ rb_block_call(obj, id_each, 0, 0, cycle_i, ary);
+ for (;;) {
+ for (i=0; i<RARRAY_LEN(ary); i++) {
+ rb_yield(RARRAY_PTR(ary)[i]);
+ }
+ }
+ return Qnil;
+}
+
/*
* The <code>Enumerable</code> mixin provides collection classes with
* several traversal and searching methods, and with the ability to
@@ -1578,6 +1616,7 @@ Init_Enumerable(void)
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, -1);
rb_define_method(rb_mEnumerable, "drop", enum_drop, -1);
+ rb_define_method(rb_mEnumerable, "cycle", enum_cycle, 0);
id_eqq = rb_intern("===");
id_each = rb_intern("each");