Skip to content

Instantly share code, notes, and snippets.

@chadjemmett
Created July 5, 2012 18:17
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 chadjemmett/3055436 to your computer and use it in GitHub Desktop.
Save chadjemmett/3055436 to your computer and use it in GitHub Desktop.
reverse a string without reverse method.
# the variables. a forward string
#the backward empty string.
#the holding array
forwards = "hello this is a string. In a moment it will be reversed without using the 'reverse' method."
backwards = ""
holding_array = []
#put the string in the array
forwards.each_char {|letter| holding_array.push(letter)}
#it's first in first out. We push the letters one by one into an array. then we pop them back out into the backwards string
holding_array.length.times {|letters| backwards << holding_array.pop}
puts forwards
puts backwards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment