Created
March 16, 2014 18:44
-
-
Save mikrofusion/9587917 to your computer and use it in GitHub Desktop.
reverse array (ruby)
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
#!/usr/bin/ruby | |
class Array | |
def reverse_recursive(arr) | |
if arr.length == 1 then return arr end | |
a = arr.shift | |
reverse_recursive(arr) << a | |
end | |
def reverse | |
reverse_recursive(self) | |
end | |
end | |
puts "original array: #{ARGV.inspect}" | |
puts "reversed array: #{ARGV.reverse.inspect}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment