Conditional Branching Issues

Posted by Zack on Stack Overflow See other posts from Stack Overflow or by Zack
Published on 2014-06-08T03:18:09Z Indexed on 2014/06/08 3:24 UTC
Read the original article Hit count: 114

Filed under:
|
|

Here is the code:

def main_menu
    print_main_menu
    user_selected  = gets.chomp
    if user_selected.downcase == "no"
        main_menu
    elsif user_selected == "1" || "2" || "3" || "4" || "5" || "6" || "7"
        user_selected = user_selected.to_i
        call_option(user_selected)
    else
        main_menu
    end
end

This code uses calls to allow a user to make a selection from a main menu. Depending on the input, be it a certain number, a certain word, or something else, the respective method is called (in the case of a valid input) or the main menu is printed again (in the case of an invalid input or "no").

My questions are twofold.

1) Is there an efficient way to get rid of the literal string error that appears as a result of this redundant or statement on the elsif line? (the code itself works fine, but this error appears and is frustrating).

2) When an alternate/unspecified input is made by the user, the else branch doesn't execute and main_method doesn't start over. I have no idea why this is happening. Is there something I'm missing here?

Thanks

© Stack Overflow or respective owner

Related posts about ruby

Related posts about string