Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created February 6, 2013 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fguillen/4721938 to your computer and use it in GitHub Desktop.
Save fguillen/4721938 to your computer and use it in GitHub Desktop.
Ruby, how to shuffle one array into another
# 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