From: Bill Atkins Date: 2005-04-22T23:57:04+09:00 Subject: Re: [QUIZ] HighLine (#29) ------=_Part_5235_16065989.1114181803442 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Disregard. :) On 4/22/05, Bill Atkins wrote: >=20 > Is using a class instead of a module allowed? >=20 > On 4/22/05, Ruby Quiz wrote:=20 > >=20 > > The three rules of Ruby Quiz: > >=20 > > 1. Please do not post any solutions or spoiler discussion for this quiz= =20 > > until=20 > > 48 hours have passed from the time on this message. > >=20 > > 2. Support Ruby Quiz by submitting ideas as often as you can: > >=20 > > http://www.rubyquiz.com/ > >=20 > > 3. Enjoy! > >=20 > >=20 > > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-= =3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D= -=3D-=3D-=3D-=3D > >=20 > > When you stop and think about it, methods like gets(), while handy, are= =20 > > still > > pretty low level. In running Ruby Quiz I'm always seeing solutions with= =20 > > helper=20 > > methods similar to this: > >=20 > > # by Markus Koenig > >=20 > > def ask(prompt) > > loop do > > print prompt, ' ' > > $stdout.flush > > s =3D gets > > exit if s =3D=3D nil > > s.chomp! > > if s =3D=3D 'y' or s =3D=3D 'yes' > > return true > > elsif s =3D=3D 'n' or s =3D=3D 'no' > > return false > > else > > $stderr.puts "Please answer yes or no." > > end > > end > > end > >=20 > > Surely we can make something like that better! We don't always need=20 > > Rails or a > > GUI framework and there's no reason writing a command-line application= =20 > > can't be=20 > > equally smooth. > >=20 > > This week's Ruby Quiz is to start a module called HighLine (for=20 > > high-level, > > line-oriented interface). Ideally this module would eventually cover=20 > > many > > aspects of terminal interaction, but for this quiz we'll just focus on= =20 > > getting=20 > > input. > >=20 > > What I really think we need here is to take a page out of the optparse= =20 > > book. > > Here are some general ideas: > >=20 > > age =3D ask("What is your age?", Integer, :within =3D> 0..105) > > num =3D eval "0b#{ ask( 'Enter a binary number.',=20 > > String, :validate =3D> /^[01_]+$/ ) }" > >=20 > > if ask_if("Would you like to continue?") # ... > >=20 > > None of these ideas are etched in stone. Feel free to call your input= =20 > > method > > prompt() or use a set of classes. Rework the interface any way you like= .=20 > > Just=20 > > be sure to tell us how to use your system. > >=20 > > The goal is to provide an easy-to-use, yet robust method of requesting= =20 > > input. > > It should free the programmer of common concerns like calls to chomp()= =20 > > and > > ensuring valid input.=20 > >=20 > >=20 >=20 >=20 > --=20 > Bill Atkins=20 --=20 Bill Atkins ------=_Part_5235_16065989.1114181803442--