Skip to content

Instantly share code, notes, and snippets.

@shinokada
Last active August 29, 2015 14:00
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 shinokada/11108157 to your computer and use it in GitHub Desktop.
Save shinokada/11108157 to your computer and use it in GitHub Desktop.
Swap two items in an array
a = [1, 2, 3, 4]
(0...arr.length - 1).map{|i| arr.dup.tap{ |a| a[i, 2] = a[i, 2].reverse } }
# => [[2, 1, 3, 4, 5], [1, 3, 2, 4, 5], [1, 2, 4, 3, 5], [1, 2, 3, 5, 4]]
a = [1, 2, 3]
# => [1, 2, 3]
(0...a.length - 1).map{|i| a.dup.tap{|a| a[i, 2] = a[i, 2].reverse}}
# => [[2, 1, 3], [1, 3, 2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment