Created
February 6, 2013 11:06
-
-
Save fguillen/4721938 to your computer and use it in GitHub Desktop.
Ruby, how to shuffle one array into another
This file contains hidden or 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
# Inspired in the answers: http://stackoverflow.com/questions/4908413/how-to-initialize-an-array-in-one-step-using-ruby | |
class Array | |
def shuffle_into(array) | |
a1 = self.dup | |
a2 = array.dup | |
(a1.size + a2.size).times.map do | |
[true, false].sample ? (a1.shift || a2.shift) : (a2.shift || a1.shift) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment