有人可以简要解释一下这一行发生了什么:
new_word += alphabet[alphabet.index(i.downcase) - num]
new_word = new_word 变量的当前状态 + 什么?
这是整个程序:
def cipher(word, num)
alphabet = ('a'..'z').to_a.concat(('A'..'Z').to_a)
new_word = ""
word.each_char do |i|
if !alphabet.include?(i)
new_word +=i
else
new_word += alphabet[alphabet.index(i.downcase) - num]
end
end
return new_word.downcase.capitalize
end
puts cipher("Apples? and Oranges!", 2)