Created
July 18, 2011 02:13
-
-
Save pasberth/1088401 to your computer and use it in GitHub Desktop.
shuffles array method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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