Skip to content

Commit 727e607

Browse files
committed
Write the detail doc of Enumerable#sample
[ci skip]
1 parent 1a5a710 commit 727e607

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
@@ -1516,7 +1516,32 @@ enum_sample_multiple_unweighted(VALUE obj, long size, VALUE random, int replace_
15161516
}
15171517

15181518
/* call-seq:
1519-
* enum.sample(n=1, random: Random, replace: false)
1519+
* enum.sample -> obj
1520+
* enum.sample(random: rng) -> obj
1521+
* enum.sample(n) -> ary
1522+
* enum.sample(n, random: rng) -> ary
1523+
* enum.sample(n, random: rng, replace: true) -> ary
1524+
*
1525+
* Choose a random element or +n+ random elements from the enumerable.
1526+
*
1527+
* The enumerable is completely scanned just once for choosing random elements
1528+
* even if +n+ is ommitted or +n+ is +1+.
1529+
*
1530+
* +replace:+ keyword specifies whether the sample is with or without
1531+
* replacement.
1532+
*
1533+
* On without-replacement sampling, the elements are chosen by using random
1534+
* in order to ensure that an element doesn't repeat itself unless the
1535+
* enumerable already contained duplicated elements.
1536+
*
1537+
* On with-replacement sampling, the elements are chosen by using random, and
1538+
* indices into the array can be duplicated even if the enumerable didn't contain
1539+
* duplicated elements.
1540+
*
1541+
* If the enumerable is empty the first two forms return +nil+, and the latter
1542+
* forms with +n+ return an empty array.
1543+
*
1544+
* The optional +rng+ argument will be used as the random number generator.
15201545
*/
15211546
static VALUE
15221547
enum_sample(int argc, VALUE *argv, VALUE obj)

0 commit comments

Comments
 (0)