Ruby 中的"end"语句



我有一个if, elseelse if语句,我正在使用Ruby的官方文档,但是我不知道在哪里放置我的end语句。

代码如下:

class Menu
  def principal_menu
    user_input = gets
    #On supprime le n du retour à la ligne
    user_input = user_input.chomp
    if user_input == "3"
      exit
    else if user_input == "1"
      if File.exists?("accounts.txt")
    else 
      File::new("accounts.txt","w+")
    end
    else if user_input == "2"
      new_account = account.new
    end
  end
end

错误:AccountManager.rb:63: syntax error, unexpected end-of-input, expecting keyword_end

注意:第63行是文件的最末尾。

有谁能帮助一个Ruby新手:D

谢谢!

你的问题是:

class Menu
    def principal_menu
        user_input = gets
        #On supprime le n du retour à la ligne
        user_input = user_input.chomp
        if user_input == "3"
            exit
        else if user_input == "1"
            if File.exists?("accounts.txt")
                # you aren't doing anything here
            else 
                File::new("accounts.txt","w+")
            end
        else if user_input == "2"
            new_account = account.new
        end
    end
end
在Ruby中,您使用elsif而不是else if。这可能会难倒刚接触ruby的人。

class Menu
    def principal_menu
        user_input = gets
        #On supprime le n du retour à la ligne
        user_input = user_input.chomp
        if user_input == "3"
            exit
        elsif user_input == "1"
            if File.exists?("accounts.txt")
            else 
                File::new("accounts.txt","w+")
            end
        else user_input == "2"
            new_account = account.new
        end
    end
end

最新更新