Andromelus Andromelus - 3 months ago 6
Ruby Question

The "end" statement in Ruby

I have a

if
,
else
, and
else if
statement, and I'm using the official documentation for Ruby, but I can't figure out where to put my
end
statements.

Here is the code:

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


The error is:
AccountManager.rb:63: syntax error, unexpected end-of-input, expecting keyword_end


Note: Line 63 is the very end of the file.

Could some one help a newbie in Ruby :D

Thanks!

Answer

Here's your problem:

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

In Ruby, you use elsif instead of else if. This can stump new-to-Ruby people.

This is correct:

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
Comments