Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created July 18, 2011 02:13
Show Gist options
  • Save pasberth/1088401 to your computer and use it in GitHub Desktop.
Save pasberth/1088401 to your computer and use it in GitHub Desktop.
shuffles array method.
# Array#shuffle returns to shuffled self.
# Array#shuffle! destructive shuffles to self.
# [1, 2, 3, 4, 5].shuffle # => [2, 1, 5, 4, 3] (example)
class Array
def shuffle
clone.shuffle!
end
def shuffle!
tmp = clone
clear
push(tmp.delete(tmp.sample)) until tmp.empty?
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment