summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurdette Lamar <BurdetteLamar@Yahoo.com>2021-10-01 17:51:39 -0500
committerGitHub <noreply@github.com>2021-10-01 17:51:39 -0500
commit8c10fd2583a949b131a69e639444cd264936164d (patch)
tree4652af1387a4f84c7357b873e3ff001d1cc372cd
parent27d9935d516a77a7b8c245398befd8b2dd059dcd (diff)
Enhanced RDoc for Enumerable (#4922)
Treated: #drop #drop_while #cycle
Notes
Notes: Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>
-rw-r--r--enum.c75
1 files changed, 46 insertions, 29 deletions
diff --git a/enum.c b/enum.c
index cac9597d67..7528eb88c0 100644
--- a/enum.c
+++ b/enum.c
@@ -3290,11 +3290,11 @@ take_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
* For non-negative integer +n+, returns the first +n+ elements:
*
* r = (1..4)
- * r.take(2) # => [1, 2]
- * r.take(0) # => []
+ * r.take(2) # => [1, 2]
+ * r.take(0) # => []
*
* h = {foo:0, bar: 1, baz: 2, bat: 3}
- * h.take(2) # => [[:foo, 0], [:bar, 1]]
+ * h.take(2) # => [[:foo, 0], [:bar, 1]]
*
*/
@@ -3338,9 +3338,10 @@ take_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ary))
* (1..4).take_while{|i| i < 3 } # => [1, 2]
* h = {foo: 0, bar: 1, baz: 2}
* a = h.take_while{|element| key, value = *element; value < 2 }
- * a # => [[:foo, 0], [:bar, 1]]
+ * a # => [[:foo, 0], [:bar, 1]]
*
* With no block given, returns an Enumerator.
+ *
*/
static VALUE
@@ -3369,13 +3370,20 @@ drop_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
/*
* call-seq:
- * enum.drop(n) -> array
+ * drop(n) -> array
+ *
+ * For positive integer +n+, returns an array containing
+ * all but the first +n+ elements:
*
- * Drops first n elements from <i>enum</i>, and returns rest elements
- * in an array.
+ * r = (1..4)
+ * r.drop(3) # => [4]
+ * r.drop(2) # => [3, 4]
+ * r.drop(1) # => [2, 3, 4]
+ * r.drop(0) # => [1, 2, 3, 4]
+ * r.drop(50) # => []
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.drop(3) #=> [4, 5, 0]
+ * h = {foo:0, bar: 1, baz: 2, bat: 3}
+ * h.drop(2) # => [[:baz, 2], [:bat, 3]]
*
*/
@@ -3414,17 +3422,20 @@ drop_while_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, args))
/*
* call-seq:
- * enum.drop_while { |obj| block } -> array
- * enum.drop_while -> an_enumerator
+ * drop_while {|element| ... } -> array
+ * drop_while -> enumerator
*
- * Drops elements up to, but not including, the first element for
- * which the block returns +nil+ or +false+ and returns an array
- * containing the remaining elements.
+ * Calls the block with successive elements as long as the block
+ * returns a truthy value;
+ * returns an array of all elements after that point:
*
- * If no block is given, an enumerator is returned instead.
*
- * a = [1, 2, 3, 4, 5, 0]
- * a.drop_while { |i| i < 3 } #=> [3, 4, 5, 0]
+ * (1..4).drop_while{|i| i < 3 } # => [3, 4]
+ * h = {foo: 0, bar: 1, baz: 2}
+ * a = h.drop_while{|element| key, value = *element; value < 2 }
+ * a # => [[:baz, 2]]
+ *
+ * With no block given, returns an Enumerator.
*
*/
@@ -3474,22 +3485,28 @@ enum_cycle_size(VALUE self, VALUE args, VALUE eobj)
/*
* call-seq:
- * enum.cycle(n=nil) { |obj| block } -> nil
- * enum.cycle(n=nil) -> an_enumerator
+ * cycle(n = nil) {|element| ...} -> nil
+ * cycle(n = nil) -> enumerator
+ *
+ * When called with positive integer argument +n+ and a block,
+ * calls the block with each element, then does so again,
+ * until it has done so +n+ times; returns +nil+:
*
- * Calls <i>block</i> for each element of <i>enum</i> repeatedly _n_
- * times or forever if none or +nil+ is given. If a non-positive
- * number is given or the collection is empty, does nothing. Returns
- * +nil+ if the loop has finished without getting interrupted.
+ * a = []
+ * (1..4).cycle(3) {|element| a.push(element) } # => nil
+ * a # => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
+ * a = []
+ * ('a'..'d').cycle(2) {|element| a.push(element) }
+ * a # => ["a", "b", "c", "d", "a", "b", "c", "d"]
+ * a = []
+ * {foo: 0, bar: 1, baz: 2}.cycle(2) {|element| a.push(element) }
+ * a # => [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]
*
- * Enumerable#cycle saves elements in an internal array so changes
- * to <i>enum</i> after the first pass have no effect.
+ * If count is zero or negative, does not call the block.
*
- * If no block is given, an enumerator is returned instead.
+ * When called with a block and +n+ is +nil+, cycles forever.
*
- * a = ["a", "b", "c"]
- * a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
- * a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
+ * When no block is given, returns an Enumerator.
*
*/