summaryrefslogtreecommitdiff
path: root/array.rb
diff options
context:
space:
mode:
authorBurdette Lamar <BurdetteLamar@Yahoo.com>2020-09-02 14:02:34 -0500
committerGitHub <noreply@github.com>2020-09-02 14:02:34 -0500
commit54fb8fb62a30c7b60ab6443a62821f6f8bc479c4 (patch)
tree6061f7094f4b6acc3e9c28ae2a17a186ce45cdc4 /array.rb
parentd9b8411a7be4c9e300b75bc374f29e6965ab3040 (diff)
Comply with guide for method doc: array.c (#3506)
Methods: any? all? one? none? sum shuffle! shuffle sample
Notes
Notes: Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>
Diffstat (limited to 'array.rb')
-rw-r--r--array.rb71
1 files changed, 34 insertions, 37 deletions
diff --git a/array.rb b/array.rb
index 9de9634835..36f4f0afa5 100644
--- a/array.rb
+++ b/array.rb
@@ -1,59 +1,56 @@
class Array
# call-seq:
- # ary.shuffle! -> ary
- # ary.shuffle!(random: rng) -> ary
+ # array.shuffle!(random: Random) -> array
#
- # Shuffles elements in +self+ in place.
- #
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
- # a.shuffle! #=> [2, 3, 1]
- # a #=> [2, 3, 1]
- #
- # The optional +rng+ argument will be used as the random number generator.
+ # Shuffles the elements of +self+ in place.
+ # a = [1, 2, 3] #=> [1, 2, 3]
+ # a.shuffle! #=> [2, 3, 1]
#
+ # The optional +random+ argument will be used as the random number generator:
# a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
def shuffle!(random: Random)
Primitive.rb_ary_shuffle_bang(random)
end
# call-seq:
- # ary.shuffle -> new_ary
- # ary.shuffle(random: rng) -> new_ary
+ # array.shuffle(random: Random) -> new_ary
#
# Returns a new array with elements of +self+ shuffled.
+ # a = [1, 2, 3] #=> [1, 2, 3]
+ # a.shuffle #=> [2, 3, 1]
#
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
- # a.shuffle #=> [2, 3, 1]
- # a #=> [1, 2, 3]
- #
- # The optional +rng+ argument will be used as the random number generator.
- #
+ # The optional +random+ argument will be used as the random number generator:
# a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
def shuffle(random: Random)
Primitive.rb_ary_shuffle(random)
end
# call-seq:
- # ary.sample -> obj
- # ary.sample(random: rng) -> obj
- # ary.sample(n) -> new_ary
- # ary.sample(n, random: rng) -> new_ary
- #
- # Choose a random element or +n+ random elements from the array.
- #
- # The elements are chosen by using random and unique indices into the array
- # in order to ensure that an element doesn't repeat itself unless the array
- # already contained duplicate elements.
- #
- # If the array is empty the first form returns +nil+ and the second form
- # returns an empty array.
- #
- # a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
- # a.sample #=> 7
- # a.sample(4) #=> [6, 4, 2, 5]
- #
- # The optional +rng+ argument will be used as the random number generator.
- #
+ # array.sample(random: Random) -> object
+ # array.sample(n, random: Random) -> new_ary
+ #
+ # Returns random elements from +self+.
+ #
+ # When no arguments are given, returns a random element from +self+:
+ # a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ # a.sample # => 3
+ # a.sample # => 8
+ # If +self+ is empty, returns +nil+.
+ #
+ # When argument +n+ is given (but not keyword argument +random+),
+ # returns a new \Array containing +n+ random elements from +self+:
+ # a.sample(3) # => [8, 9, 2]
+ # a.sample(6) # => [9, 6, 10, 3, 1, 4]
+ # Returns no more than <tt>a.size</tt> elements
+ # (because no new duplicates are introduced):
+ # a.sample(a.size * 2) # => [6, 4, 1, 8, 5, 9, 10, 2, 3, 7]
+ # But +self+ may contain duplicates:
+ # a = [1, 1, 1, 2, 2, 3]
+ # a.sample(a.size * 2) # => [1, 1, 3, 2, 1, 2]
+ # Returns a new empty \Array if +self+ is empty.
+ #
+ # The optional +random+ argument will be used as the random number generator:
+ # a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# a.sample(random: Random.new(1)) #=> 6
# a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
def sample(n = (ary = false), random: Random)