這條線被投擲的錯誤:紅寶石錯誤 - 「+」:Fixnum對象的沒有隱式轉換成字符串(類型錯誤)
charLine.each_index { |i| charLine[i] = (charLine[i] + shift)}
編輯:charLine[i]
是一個數字(ASCII碼)和shift
是一個數也
我知道它與試圖將變量中的數字添加到我在charLine數組索引中的字節ascii代碼中有關。但是我還沒有處理所有這就是爲什麼我有這個錯誤如此混亂的字符串...以下是有錯誤的方法:
def caesarCipher(string)
puts "Original:\n #{string}"
charLine = string.chars
charLine.each_index { |i| charLine[i]=charLine[i].bytes }
charLine.flatten!
puts charLine.inspect
shift = 1
alphabet = ('A'..'Z').to_a
while shift <= alphabet.size
#moving up in the alphabet using ascii code
charLine.each_index { |i| charLine[i] = (charLine[i] + shift)}
#converting back to letters
charLine.each_index { |x| charLine[x] = charLine[x].chr }
puts "Shifted:\n #{charLine.inspect}"
puts "With a letter shift of #{shift}"
shift = shift + 1
@@shiftyArray.push(charLine.flatten)
end
end
美麗!謝謝,完全解決了一切:)我需要更多地研究地圖方法 – Gcap 2015-03-03 03:07:29