Skip to content

Instantly share code, notes, and snippets.

@psylone
Created January 28, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psylone/90dd7746db15a1e1d8c8 to your computer and use it in GitHub Desktop.
Save psylone/90dd7746db15a1e1d8c8 to your computer and use it in GitHub Desktop.
def reverse(string)
# gets (input from STDIN) isn't necessary cause you get string argument in this method
# input = gets.strip
# You should split your string by empty string to get an array of it's chars
string_array = string.split('')
# Class name should be started from a capital letter
new_word = Array.new
# You get only last letter here, but it's probably should be in loop
# last_letter = string_array.pop
#
# if string_array.length > 0
# new_word.push(last_letter)
# else return new_word.join
# end
while letter = string_array.pop
new_word.push(letter)
end
# In Ruby you can ommit return keyword if you just want to return the last value
new_word.join
end
p reverse("Look at me, I am reversed!") # => "!desrever ma I ,em ta kooL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment