Trapping errors, a simple example

Discussion in 'Ruby' started by John Maclean, Jan 5, 2006.

  1. John Maclean

    John Maclean Guest

    Hey Chaps,

    Here's a really simple menu with only 4 options. I'd like to be able do some simple error trapping... How's it done?

    #!/usr/bin/ruby
    #Wed Jan 4 06:17:22 GMT 2006
    class TopMenu
    def welcome
    puts "Welcome to de-blah-de-blah-de-blah"
    puts "~~~~~~~ ~~ ~~ ~~~~ ~~ ~~~~ ~~ ~~~~~"
    end

    def options
    # we will use this data to create a class called Option
    # each option will have a new menu
    # and will be a child class of the main one
    print "------------------------------------\nmenu - Which stage are you at?\n------------------------------------\n0:- Before site work\n1:- During site work\n2:- After site work\n3:- Other stuff\nq:- quit!\n"
    end

    # grab the input
    def topchoice
    print "enter your choice (0,1,2,q) : "
    p = $stdin.gets # TODO:- check it's validity # TODO:- is the choice a number in the correct range? # TODO:- does he wanna quit?
    print "you have chosen #{p}"
    if p == "0"
    print "you have chosen #{p}"
    else
    if p == "q"
    print "you have chosen #{p}"
    end
    end
    end




    # create a new instance
    top = TopMenu.new
    top.welcome
    top.options
    top.topchoice


    --
    John Maclean
    MSc (DIC)
    07739 171 531
    John Maclean, Jan 5, 2006
    #1
    1. Advertising

  2. John Maclean wrote:
    > Hey Chaps,
    >
    > Here's a really simple menu with only 4 options. I'd like to be able
    > do some simple error trapping... How's it done?
    >
    > #!/usr/bin/ruby
    > #Wed Jan 4 06:17:22 GMT 2006
    > class TopMenu
    > def welcome
    > puts "Welcome to de-blah-de-blah-de-blah"
    > puts "~~~~~~~ ~~ ~~ ~~~~ ~~ ~~~~ ~~ ~~~~~"
    > end
    >
    > def options
    > # we will use this data to create a class called Option
    > # each option will have a new menu
    > # and will be a child class of the main one
    > print "------------------------------------\nmenu - Which stage
    > are you at?\n------------------------------------\n0:- Before site
    > work\n1:- During site work\n2:- After site work\n3:- Other stuff\nq:-
    > quit!\n" end
    >
    > # grab the input
    > def topchoice
    > print "enter your choice (0,1,2,q) : "
    > p = $stdin.gets # TODO:- check it's validity # TODO:- is the


    p.chomp!

    if /\a[012q]\Z/
    # ok
    case p
    when 0
    when 1
    ....
    end
    else
    # error
    end

    HTH

    robert

    > choice a number in the correct range? # TODO:- does he wanna
    > quit? print "you have chosen #{p}" if p == "0"
    > print "you have chosen #{p}"
    > else
    > if p == "q"
    > print "you have chosen #{p}"
    > end
    > end
    > end
    >
    >
    >
    >
    > # create a new instance
    > top = TopMenu.new
    > top.welcome
    > top.options
    > top.topchoice
    Robert Klemme, Jan 5, 2006
    #2
    1. Advertising

  3. On Jan 5, 2006, at 10:37 AM, Robert Klemme wrote:

    > John Maclean wrote:
    >> Hey Chaps,
    >>
    >> Here's a really simple menu with only 4 options. I'd like to be able
    >> do some simple error trapping... How's it done?
    >>
    >> #!/usr/bin/ruby
    >> #Wed Jan 4 06:17:22 GMT 2006
    >> class TopMenu
    >> def welcome
    >> puts "Welcome to de-blah-de-blah-de-blah"
    >> puts "~~~~~~~ ~~ ~~ ~~~~ ~~ ~~~~ ~~ ~~~~~"
    >> end
    >>
    >> def options
    >> # we will use this data to create a class called Option
    >> # each option will have a new menu
    >> # and will be a child class of the main one
    >> print "------------------------------------\nmenu - Which stage
    >> are you at?\n------------------------------------\n0:- Before site
    >> work\n1:- During site work\n2:- After site work\n3:- Other stuff\nq:-
    >> quit!\n" end
    >>
    >> # grab the input
    >> def topchoice
    >> print "enter your choice (0,1,2,q) : "
    >> p = $stdin.gets # TODO:- check it's validity # TODO:- is the

    >
    > p.chomp!
    >
    > if /\a[012q]\Z/
    > # ok
    > case p
    > when 0
    > when 1
    > ....
    > end
    > else
    > # error
    > end


    You might also want to take a look at HighLine[1], which can make
    this kind of user interaction easier.

    1: http://rubyforge.org/projects/highline/

    James Edward Gray II
    James Edward Gray II, Jan 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbA==?=

    Trapping 'compile' errors when aspx page is rendered.

    =?Utf-8?B?QmlsbA==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    506
    =?Utf-8?B?QmlsbA==?=
    Mar 29, 2005
  2. Aaron Fude

    Trapping JNI errors

    Aaron Fude, Jun 1, 2004, in forum: Java
    Replies:
    4
    Views:
    1,489
    Gordon Beaton
    Jun 2, 2004
  3. Avi Kak
    Replies:
    6
    Views:
    311
    Duncan Booth
    Feb 13, 2006
  4. Eitan

    Trapping errors in ASP

    Eitan, Jan 12, 2005, in forum: ASP General
    Replies:
    6
    Views:
    126
    Bob Barrows [MVP]
    Jan 13, 2005
  5. Eitan
    Replies:
    3
    Views:
    129
    Bob Barrows [MVP]
    Jan 12, 2005
Loading...

Share This Page