Created
February 21, 2013 02:31
-
-
Save nimamehanian/5001510 to your computer and use it in GitHub Desktop.
Reverse the order of an array's elements, without using Ruby's built-in methods.
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
class Array | |
def backwards | |
length = self.length | |
i = 1 | |
while i != length | |
i += 1 | |
value = self.slice!(length - i) | |
self << value | |
break if self[length - i] == self[0] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment