Skip to content
Permalink
Browse files

Merge pull request #516 from jstepien/array-shuffle

Array#shuffle without arguments
  • Loading branch information
alex committed Mar 30, 2013
2 parents e42035a + ec6f9a4 commit 5033a7177006149bbc08911b24026cf1a087800d
Showing with 14 additions and 5 deletions.
  1. +14 −0 lib-topaz/array.rb
  2. +0 −5 spec/tags/core/array/shuffle_tags.txt
@@ -309,4 +309,18 @@ def rotate(n = 1)
Array.new(self).rotate!(n)
end

def shuffle!
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
(self.length - 1).downto(1) do |idx|
other = rand(idx + 1)
self[other], self[idx] = self[idx], self[other]
end
self
end

def shuffle
arr = Array.new(self)
arr.shuffle!
arr
end
end
@@ -1,8 +1,3 @@
fails:Array#shuffle returns the same values, in a usually different order
fails:Array#shuffle is not destructive
fails:Array#shuffle does not return subclass instances with Array subclass
fails:Array#shuffle! returns the same values, in a usually different order
fails:Array#shuffle! raises a RuntimeError on a frozen array
fails:Array#shuffle attempts coercion via #to_hash
fails:Array#shuffle uses default random generator
fails:Array#shuffle uses given random generator

0 comments on commit 5033a71

Please sign in to comment.