Skip to content

Commit 59449de

Browse files
committed
Write the detail doc of Enumerable#sample
[ci skip]
1 parent 70eadf3 commit 59449de

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

ext/enumerable/statistics/extension/statistics.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,32 @@ enum_sample_multiple_unweighted(VALUE obj, long size, VALUE random, int replace_
15361536
}
15371537

15381538
/* call-seq:
1539-
* enum.sample(n=1, random: Random, replace: false)
1539+
* enum.sample -> obj
1540+
* enum.sample(random: rng) -> obj
1541+
* enum.sample(n) -> ary
1542+
* enum.sample(n, random: rng) -> ary
1543+
* enum.sample(n, random: rng, replace: true) -> ary
1544+
*
1545+
* Choose a random element or +n+ random elements from the enumerable.
1546+
*
1547+
* The enumerable is completely scanned just once for choosing random elements
1548+
* even if +n+ is ommitted or +n+ is +1+.
1549+
*
1550+
* +replace:+ keyword specifies whether the sample is with or without
1551+
* replacement.
1552+
*
1553+
* On without-replacement sampling, the elements are chosen by using random
1554+
* in order to ensure that an element doesn't repeat itself unless the
1555+
* enumerable already contained duplicated elements.
1556+
*
1557+
* On with-replacement sampling, the elements are chosen by using random, and
1558+
* indices into the array can be duplicated even if the enumerable didn't contain
1559+
* duplicated elements.
1560+
*
1561+
* If the enumerable is empty the first two forms return +nil+, and the latter
1562+
* forms with +n+ return an empty array.
1563+
*
1564+
* The optional +rng+ argument will be used as the random number generator.
15401565
*/
15411566
static VALUE
15421567
enum_sample(int argc, VALUE *argv, VALUE obj)

0 commit comments

Comments
 (0)