summaryrefslogtreecommitdiff
path: root/enum.c
diff options
context:
space:
mode:
authorBurdette Lamar <[email protected]>2021-09-28 11:38:35 -0500
committer<[email protected]>2021-09-28 11:38:35 -0500
commit1e10099e0919eda9464ef454b4920213eca658f3 ()
tree68a9eac321a6ad8441223c6714a83bb4cba0d66b /enum.c
parent545e01645f7350c255f79b90f184a1317d3d55fb (diff)
Enhanced RDoc for Enumerable (#4906)
Treats: #partition #group_by #tally #first #sort #sort_by #all? #any?
Notes: Merged-By: BurdetteLamar <[email protected]>
-rw-r--r--enum.c264
1 files changed, 176 insertions, 88 deletions
@@ -1066,16 +1066,29 @@ partition_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, arys))
/*
* call-seq:
- * enum.partition { |obj| block } -> [ true_array, false_array ]
- * enum.partition -> an_enumerator
*
- * Returns two arrays, the first containing the elements of
- * <i>enum</i> for which the block evaluates to true, the second
- * containing the rest.
*
- * If no block is given, an enumerator is returned instead.
*
- * (1..6).partition { |v| v.even? } #=> [[2, 4, 6], [1, 3, 5]]
*
*/
@@ -1114,16 +1127,23 @@ group_by_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
/*
* call-seq:
- * enum.group_by { |obj| block } -> a_hash
- * enum.group_by -> an_enumerator
*
- * Groups the collection by result of the block. Returns a hash where the
- * keys are the evaluated result from the block and the values are
- * arrays of elements in the collection that correspond to the key.
*
- * If no block is given an enumerator is returned.
*
- * (1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
*
*/
@@ -1173,18 +1193,30 @@ tally_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
/*
* call-seq:
- * enum.tally -> a_hash
- * enum.tally(a_hash) -> a_hash
*
- * Tallies the collection, i.e., counts the occurrences of each element.
- * Returns a hash with the elements of the collection as keys and the
- * corresponding counts as values.
*
- * ["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}
*
- * If a hash is given, the number of occurrences is added to each value
- * in the hash, and the hash is returned. The value corresponding to
- * each element must be an integer.
*/
static VALUE
@@ -1219,18 +1251,26 @@ static VALUE enum_take(VALUE obj, VALUE n);
/*
* call-seq:
- * enum.first -> obj or nil
- * enum.first(n) -> an_array
*
- * Returns the first element, or the first +n+ elements, of the enumerable.
- * If the enumerable is empty, the first form returns <code>nil</code>, and the
- * second form returns an empty array.
*
- * %w[foo bar baz].first #=> "foo"
- * %w[foo bar baz].first(2) #=> ["foo", "bar"]
- * %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"]
- * [].first #=> nil
- * [].first(10) #=> []
*
*/
@@ -1249,28 +1289,35 @@ enum_first(int argc, VALUE *argv, VALUE obj)
}
}
-
/*
* call-seq:
- * enum.sort -> array
- * enum.sort { |a, b| block } -> array
*
- * Returns an array containing the items in <i>enum</i> sorted.
*
- * Comparisons for the sort will be done using the items' own
- * <code><=></code> operator or using an optional code block.
*
- * The block must implement a comparison between +a+ and +b+ and return
- * an integer less than 0 when +b+ follows +a+, +0+ when +a+ and +b+
- * are equivalent, or an integer greater than 0 when +a+ follows +b+.
*
- * The result is not guaranteed to be stable. When the comparison of two
- * elements returns +0+, the order of the elements is unpredictable.
*
- * %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
- * (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
*
- * See also Enumerable#sort_by. It implements a Schwartzian transform
* which is useful when key computation or comparison is expensive.
*/
@@ -1335,19 +1382,23 @@ sort_by_cmp(const void *ap, const void *bp, void *data)
/*
* call-seq:
- * enum.sort_by { |obj| block } -> array
- * enum.sort_by -> an_enumerator
*
- * Sorts <i>enum</i> using a set of keys generated by mapping the
- * values in <i>enum</i> through the given block.
*
- * The result is not guaranteed to be stable. When two keys are equal,
- * the order of the corresponding elements is unpredictable.
*
- * If no block is given, an enumerator is returned instead.
*
- * %w{apple pear fig}.sort_by { |word| word.length }
- * #=> ["fig", "pear", "apple"]
*
* The current implementation of #sort_by generates an array of
* tuples containing the original collection element and the mapped
@@ -1505,25 +1556,44 @@ DEFINE_ENUMFUNCS(all)
/*
* call-seq:
- * enum.all? [{ |obj| block } ] -> true or false
- * enum.all?(pattern) -> true or false
*
- * Passes each element of the collection to the given block. The method
- * returns <code>true</code> if the block never returns
- * <code>false</code> or <code>nil</code>. If the block is not given,
- * Ruby adds an implicit block of <code>{ |obj| obj }</code> which will
- * cause #all? to return +true+ when none of the collection members are
- * +false+ or +nil+.
*
- * If instead a pattern is supplied, the method returns whether
- * <code>pattern === element</code> for every collection member.
*
- * %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
- * %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
- * %w[ant bear cat].all?(/t/) #=> false
- * [1, 2i, 3.14].all?(Numeric) #=> true
- * [nil, true, 99].all? #=> false
- * [].all? #=> true
*
*/
@@ -1547,26 +1617,44 @@ DEFINE_ENUMFUNCS(any)
/*
* call-seq:
- * enum.any? [{ |obj| block }] -> true or false
- * enum.any?(pattern) -> true or false
*
- * Passes each element of the collection to the given block. The method
- * returns <code>true</code> if the block ever returns a value other
- * than <code>false</code> or <code>nil</code>. If the block is not
- * given, Ruby adds an implicit block of <code>{ |obj| obj }</code> that
- * will cause #any? to return +true+ if at least one of the collection
- * members is not +false+ or +nil+.
*
- * If instead a pattern is supplied, the method returns whether
- * <code>pattern === element</code> for any collection member.
*
- * %w[ant bear cat].any? { |word| word.length >= 3 } #=> true
- * %w[ant bear cat].any? { |word| word.length >= 4 } #=> true
- * %w[ant bear cat].any?(/d/) #=> false
- * [nil, true, 99].any?(Integer) #=> true
- * [nil, true, 99].any? #=> true
- * [].any? #=> false
*
*/
static VALUE