Created
May 24, 2013 23:26
-
-
Save LogaJ/5647192 to your computer and use it in GitHub Desktop.
Recursion play
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
def append(ary, n) | |
return ary if n < 0 | |
return append((ary.push n), n-1) | |
end | |
appended = append [], 2 | |
puts appended.inspect # => [2, 1, 0] | |
def prepend(ary, n) | |
return ary if n < 0 | |
return prepend((ary.unshift n), n-1) | |
end | |
prepended = prepend [], 2 | |
puts prepended.inspect # => [0, 1, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment