summaryrefslogtreecommitdiff
path: root/array.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 /array.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 'array.c')
-rw-r--r--array.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/array.c b/array.c
index 67f472a826..3bb07e679c 100644
--- a/array.c
+++ b/array.c
@@ -2951,6 +2951,31 @@ rb_ary_choice(VALUE ary)
}
+/*
+ * call-seq:
+ * ary.cycle {|obj| block }
+ *
+ * Calls <i>block</i> repeatedly forever.
+ *
+ * a = ["a", "b", "c"]
+ * a.each {|x| puts x } # print, a, b, c, a, b, c,.. forever.
+ *
+ */
+
+static VALUE
+rb_ary_cycle(VALUE ary)
+{
+ long i;
+
+ RETURN_ENUMERATOR(ary, 0, 0);
+ for (;;) {
+ for (i=0; i<RARRAY_LEN(ary); i++) {
+ rb_yield(RARRAY_PTR(ary)[i]);
+ }
+ }
+ return Qnil;
+}
+
/* Arrays are ordered, integer-indexed collections of any object.
* Array indexing starts at 0, as in C or Java. A negative index is
* assumed to be relative to the end of the array---that is, an index of -1
@@ -3048,6 +3073,7 @@ Init_Array(void)
rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);
rb_define_method(rb_cArray, "choice", rb_ary_choice, 0);
+ rb_define_method(rb_cArray, "cycle", rb_ary_cycle, 0);
id_cmp = rb_intern("<=>");
}