From 279b2b5b600f0bb16f7ebb08aa4a299cf7b023a8 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Tue, 5 Oct 2021 10:52:51 -0500 Subject: Enhanced RDoc for Enumerable#slice_before (#4932) * Enhanced RDoc for Enumerable#slice_before * Enhanced RDoc for Enumerable#slice_before --- enum.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'enum.c') diff --git a/enum.c b/enum.c index 96cb708256..05c2201992 100644 --- a/enum.c +++ b/enum.c @@ -3796,24 +3796,41 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator)) /* * call-seq: - * enum.slice_before(pattern) -> an_enumerator - * enum.slice_before { |elt| bool } -> an_enumerator + * slice_before(pattern) -> enumerator + * slice_before {|array| ... } -> enumerator * - * Creates an enumerator for each chunked elements. - * The beginnings of chunks are defined by _pattern_ and the block. - - * If _pattern_ === _elt_ returns true or the block - * returns true for the element, the element is beginning of a - * chunk. - - * The === and _block_ is called from the first element to the last - * element of _enum_. The result for the first element is ignored. - - * The result enumerator yields the chunked elements as an array. - * So +each+ method can be called as follows: + * With argument +pattern+, returns an enumerator that uses the pattern + * to partition elements into arrays. + * An element begins a new slice if element === pattern + * (or if it is the first element). * - * enum.slice_before(pattern).each { |ary| ... } - * enum.slice_before { |elt| bool }.each { |ary| ... } + * a = %w[foo bar fop for baz fob fog bam foy] + * e = a.slice_before(/ba/) # => # + * e.each {|array| p array } + * + * Output: + * + * ["foo"] + * ["bar", "fop", "for"] + * ["baz", "fob", "fog"] + * ["bam", "foy"] + * + * With a block, returns an enumerator that uses the block + * to partition elements into arrays. + * An element begins a new slice if its block return is a truthy value + * (or if it is the first element): + * + * e = (1..20).slice_before {|i| i % 4 == 2 } # => # + * e.each {|array| p array } + * + * Output: + * + * [1] + * [2, 3, 4, 5] + * [6, 7, 8, 9] + * [10, 11, 12, 13] + * [14, 15, 16, 17] + * [18, 19, 20] * * Other methods of the Enumerator class and Enumerable module, * such as +to_a+, +map+, etc., are also usable. @@ -3831,7 +3848,6 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator)) * f.slice_before { |line| /\A\S/ === line }.each { |e| pp e } * } * - * * "svn proplist -R" produces multiline output for each file. * They can be chunked as follows: * -- cgit v1.2.3