diff options
author | Burdette Lamar <[email protected]> | 2021-10-04 10:41:12 -0500 |
---|---|---|
committer | <[email protected]> | 2021-10-04 10:41:12 -0500 |
commit | 8dc546b6b6c77de3a3ea85171f63e88d21c8a3be () | |
tree | 9dda97928b640a7b31ebd2ed8c1826d7a551d82c /enum.c | |
parent | c4570acc86837fefa542a678dfdaba73cdd1fd03 (diff) |
Enhanced RDoc for Enumerable#chunk (#4930)
Notes: Merged-By: BurdetteLamar <[email protected]>
-rw-r--r-- | enum.c | 107 |
1 files changed, 62 insertions, 45 deletions
@@ -3622,50 +3622,79 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator)) /* * call-seq: - * enum.chunk { |elt| ... } -> an_enumerator * - * Enumerates over the items, chunking them together based on the return - * value of the block. * - * Consecutive elements which return the same block value are chunked together. * - * For example, consecutive even numbers and odd numbers can be - * chunked as follows. * - * [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5].chunk { |n| - * n.even? - * }.each { |even, ary| - * p [even, ary] - * } - * #=> [false, [3, 1]] - * # [true, [4]] - * # [false, [1, 5, 9]] - * # [true, [2, 6]] - * # [false, [5, 3, 5]] * - * This method is especially useful for sorted series of elements. - * The following example counts words for each initial letter. * - * open("/usr/share/dict/words", "r:iso-8859-1") { |f| - * f.chunk { |line| line.upcase.ord }.each { |ch, lines| p [ch.chr, lines.length] } * } - * #=> ["\n", 1] - * # ["A", 1327] - * # ["B", 1372] - * # ["C", 1507] - * # ["D", 791] - * # ... * - * The following key values have special meaning: - * - +nil+ and +:_separator+ specifies that the elements should be dropped. - * - +:_alone+ specifies that the element should be chunked by itself. * - * Any other symbols that begin with an underscore will raise an error: * - * items.chunk { |item| :_underscore } - * #=> RuntimeError: symbols beginning with an underscore are reserved * - * +nil+ and +:_separator+ can be used to ignore some elements. * * For example, the sequence of hyphens in svn log can be eliminated as follows: * @@ -3695,18 +3724,6 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator)) * pp lines * } * - * +:_alone+ can be used to force items into their own chunk. - * For example, you can put lines that contain a URL by themselves, - * and chunk the rest of the lines together, like this: - * - * pattern = /http/ - * open(filename) { |f| - * f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines| - * pp lines - * } - * } - * - * If no block is given, an enumerator to `chunk` is returned instead. */ static VALUE enum_chunk(VALUE enumerable) |