Forum: Ruby Can't get If statement right

Posted by Kat VS (saavykatvs)
on 2013-03-04 19:58
mainMenu = "Main Menu:

    Phone Book Application
    1.) Print Phone Book
    2.) Add Entry
    9.) Quit Application

    Please select an option:"

print mainMenu

option = gets

( if option == 1
    print "TODO: Print Phone Book"
    print mainMenu
    option = gets

     elsif option == 2
    print "TODO: Add Entry"
    print mainMenu
    option = gets

     elsif option == 9
    print "TODO: Quit Entry, type 'quit' to exit."
      quit.get
        ( if quit == "quit"
          abort("Goodbye.")
        else
          mainMenu
          option = gets
        end )

     else
    print "Error, please enter 1, 2, or 9."
    print mainMenu
    option = gets

     end )




Every time I run this it just outputs "Error, please enter 1, 2 or 9."
No matter what number I enter. What am I doing wrong?? Thanks!
Posted by Peter Hickman (Guest)
on 2013-03-04 20:03
(Received via mailing list)
gets will return a string and therefore option will be a string. "if 
option
== 2" is expecting option to be an integer

A simple fix would be

option = gets.to_i
Posted by Kat VS (saavykatvs)
on 2013-03-04 20:20
Okay, thanks! Now it is working. I figured it was thinking they were 
integers, and I tried putting quotes around the 1, 2, and 9, but that 
still didn't work either.

But, now, the if statement in the option == 9 elsif section isn't doing 
anything?

And how would I get the program to just loop over and over until the 
person quit?
Posted by Matt Mongeau (halogenandtoast)
on 2013-03-04 20:28
(Received via mailing list)
You should be getting something like:

undefined local variable or method `quit' for main:Object I'd suggest
examining that line to see what you've done wrong.

Lastly, when you use gets there will be a newline at the end of the 
input.
For example,

irb(main):001:0> foo = gets
hello
=> "hello\n"

You can use http://www.ruby-doc.org/core-2.0/String.html#method-i-chomp 
to
take care of it.
Posted by Matt Mongeau (halogenandtoast)
on 2013-03-04 20:34
(Received via mailing list)
As for your loop quandary, you should try some of these options. I'll 
leave
it to you to decide which you like best:

http://www.ruby-doc.org/core-2.0/Kernel.html#method-i-loop

or either of the solutions in the loops section of

http://en.wikibooks.org/wiki/Ruby_Programming/Synt...
Posted by Peter Hickman (Guest)
on 2013-03-04 20:36
(Received via mailing list)
Well when gets returns the string it would still contain the "\n" at the
end of the line so you would be comparing "2\n" to "2" which will not
match. The .to_i will convert as much of the string it can into an 
integer.
So a "2cats\n" will be converted to a 2. The moment .to_i encounters a 
non
digit it stops so the "\n" from the "2\n" will be ignored.

Anyhow here is a quick hack on your program. It might be of some help

mainMenu = "Main Menu:

    Phone Book Application
    1.) Print Phone Book
    2.) Add Entry
    9.) Quit Application

    Please select an option:"


option = ""

while option != "quit"
  puts mainMenu

  option = gets.chomp

  case option
  when "1"
  puts "TODO: Print Phone Book"
  when "2"
    puts "TODO: Add Entry"
  when "9"
    puts "TODO: Quit Entry, type 'quit' to exit."
    print ">"
    option = gets.chomp
  else
    puts "Error, please enter 1, 2, or 9. (you entered #{option})"
  end
end
Posted by Kat VS (saavykatvs)
on 2013-03-04 20:58
I was wondering if I should switch to a case statement.. But, I am way 
more familiar with the way if, else statements work so I was trying that 
first. Thanks a lot!
Posted by Peter Hickman (Guest)
on 2013-03-04 22:15
(Received via mailing list)
Ruby supports the if, else statements so it is fine to use it. It is 
just a
personal preference of mine. There is nothing "better" about using the
case statement (well none that I am aware of).
Posted by Robert Klemme (robert_k78)
on 2013-03-05 15:36
(Received via mailing list)
On Mon, Mar 4, 2013 at 10:15 PM, Peter Hickman
<peterhickman386@googlemail.com> wrote:
> Ruby supports the if, else statements so it is fine to use it. It is just a
> personal preference of mine. There is nothing "better" about using the case
> statement (well none that I am aware of).

Maybe that "case" conveys the notion of several equivalent choices
while "if else..." is for more general control flow.  In this case I'd
prefer "case" as well.

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.