2016-08-27 199 views
-3

我收到的時候我在bash嘗試運行這個錯誤:我試着擺弄它,現在幾個小時紅寶石錯誤代碼

39: syntax error, unexpected end-of-input, expecting keyword_end 

,但我仍然很新的學習Ruby和我完全困惑。以下是我正在處理的內容:

def encode(password) 
     i = 0 
     length = password.length 
     while i < length do 
      if letter[i] == "z" 
       letter[i]== "a" 
      else 
       letter[i] = letter[i].next 
     end 
    i += 1 
return password 

end 

def decode(encoded_password) 
      alphabet = "abcdefghijklmnopqrstuvwxyz" 
      i = 0 
      length = encoded_password.length 
      while i < length do 
       if letter == "a" 
        letter == "z" 
       else 
        encoded_password[i] = alphabet[alphabet.index(letter) - 1] 
      end 
      i += 1 
     end 
    return encoded_password 
end 

puts "What is the password?" 
password = gets.chomp 

puts "Would you like to decode or encode?" 
selection = gets.chomp 

if selection == "encode" 
    puts encode(password) 
elsif selection == "decode" 
    puts decode(password) 
end 

在此先感謝!

+0

在'encode'中添加'end',但是你想要做什麼? –

+1

當你有這樣的錯誤時,用'ruby -w'運行你的程序是有用的 - 它會告訴你缺少的「結束」所在的位置。 –

回答

1

您錯過了end我收到encode方法中的while塊。

def encode(password) 
    i = 0 
    length = password.length 
    while i < length do 
     if letter[i] == "z" 
      letter[i]== "a" 
     else 
      letter[i] = letter[i].next 
     end 
     i += 1 
    end # You missed this 
    return password 
end 

這將解決syntax error, unexpected end-of-input, expecting keyword_end錯誤,但您有代碼的其他問題。 letter參考encodedecode方法?