From: James Edward Gray II Date: 2006-01-06T01:43:14+09:00 Subject: Re: Trapping errors, a simple example 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