summaryrefslogtreecommitdiff
path: root/enumerator.c
diff options
context:
space:
mode:
authorAkinori MUSHA <knu@idaemons.org>2019-09-12 20:15:03 +0900
committerAkinori MUSHA <knu@idaemons.org>2019-09-12 20:18:53 +0900
commitac3e8834e030f62d2d313ec60cd7ed3c14c9ea5e (patch)
tree879d160a632ba279bb542451bbdd532bb0a917d3 /enumerator.c
parent775037613bffe6f90e7af510b7f46a2ac10610be (diff)
Document and test Enumerator.produce
Co-authored-by: Victor Shepelev <zverok.offline@gmail.com>
Diffstat (limited to 'enumerator.c')
-rw-r--r--enumerator.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/enumerator.c b/enumerator.c
index fcf49b3..43008bc 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -2890,6 +2890,28 @@ producer_size(VALUE obj, VALUE args, VALUE eobj)
return DBL2NUM(HUGE_VAL);
}
+/*
+ * call-seq:
+ * Enumerator.produce(initial = nil) { |val| } -> enumerator
+ *
+ * Creates an infinite enumerator from any block, just called over and
+ * over. Result of the previous iteration is passed to the next one.
+ * If +initial+ is provided, it is passed to the first iteration, and
+ * becomes the first element of the enumerator; if it is not provided,
+ * first iteration receives +nil+, and its result becomes first
+ * element of the iterator.
+ *
+ * Raising StopIteration from the block stops an iteration.
+ *
+ * Examples of usage:
+ *
+ * Enumerator.produce(1, &:succ) # => enumerator of 1, 2, 3, 4, ....
+ *
+ * Enumerator.produce { rand(10) } # => infinite random number sequence
+ *
+ * ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration }
+ * enclosing_section = ancestors.find { |n| n.type == :section }
+ */
static VALUE
enumerator_s_produce(int argc, VALUE *argv, VALUE klass)
{