Created
December 11, 2012 07:36
-
-
Save acook/4256609 to your computer and use it in GitHub Desktop.
Invert an Array
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
def invert original | |
result = Marshal.load Marshal.dump(original) | |
original.each_with_index do |row, r_index| | |
row.each_with_index do |column, c_index| | |
result[c_index][r_index] = column | |
end | |
end | |
result | |
end | |
array = [[1,2],[3,4]] | |
invert array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment