I wrote a code for translating a plain text message into a cipher text message using a simple substitution cipher (a=b, b=c, etc). Spaces were substituted with a "1".
I then wanted to make it so that I could type in a different message using 'gets'. This caused a problem which I solved by replacing "gets" with "gets.strip". If the gets was adding a space to the typed string, either in the beginning or end, why did it cause a problem? I had a way of dealing with spaces so is it really a " " or is it a nil space (for lack of a better word) or a character return or what? This is what I wrote:
base_alph =[" ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
code_alph = ["1", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"]
x=0
base_msgarray = []
code_msgarray = []
puts "type what you want to translate below"
base_msg = gets.strip # without the .strip the program doesn't run
msg_size = base_msg.length
puts "the message is translated as:"
loop do #this section populates an array with all the characters from the gets
break if x >= msg_size
base_msgarray.push(base_msg[x])
x += 1
end
# this section passes in the values of the base_msgarray to |letter| to then determine its key
# in base_alph then that key is passed into code_alph to find the translated letter which
# is then used to populate the code_msgarray
base_msgarray.each do |letter|
code_index = base_alph.index(letter)
code_msgarray.push(code_alph[code_index])
end
# this section displays the values of code_msgarray in a more text-like fashion
code_msgarray.each do |codeletter|
print codeletter
end
gets # for some reason if i dont have this here the terminal window disappears before I can read anything