summaryrefslogtreecommitdiff
path: root/array.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2020-01-26 18:27:40 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-01-26 19:40:34 +0900
commit29eb1b16028928139dcaa236beb6d351c85f434c (patch)
tree43a8ae1aa238767d4bf5443e47bafe1f1bc31cea /array.rb
parent0aa5195262d4193d3accf3e6b9bad236238b816b (diff)
Moved Array#shuffle and Array#shuffle! to rbinc
Diffstat (limited to 'array.rb')
-rwxr-xr-xarray.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/array.rb b/array.rb
new file mode 100755
index 0000000000..ed5779290f
--- /dev/null
+++ b/array.rb
@@ -0,0 +1,36 @@
+#!/usr/bin/ruby
+class Array
+ # call-seq:
+ # ary.shuffle! -> ary
+ # ary.shuffle!(random: rng) -> ary
+ #
+ # 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.
+ #
+ # a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
+ def shuffle!(random: Random)
+ __builtin_rb_ary_shuffle_bang(random)
+ end
+
+ # call-seq:
+ # ary.shuffle -> new_ary
+ # ary.shuffle(random: rng) -> 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]
+ #
+ # The optional +rng+ argument will be used as the random number generator.
+ #
+ # a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
+ def shuffle(random: Random)
+ __builtin_rb_ary_shuffle(random);
+ end
+end