Currently it outputs the reverse string using this code:
string = 'Hello World!'
my_reverse = String.new
i = 1; while i <= string.length
my_reverse << string[-i]
i+=1
end
puts my_reverse
But I want to write a method, my_reverse, which takes a string as an argument and returns the reversed string. Like so:
my_reverse("hello")
#=> "olleh"
Just wrap the code you already have in a method:
and then call it like so:
You should note that a method already exists to do this: string.reverse. Even then, this particular implementation of reverse isn’t the best.