From: "Peña, Botp" Date: 2007-07-19T11:30:07+09:00 Subject: Re: Newbie looking for help: Determining Gender From: Matthew Borgeson [mailto:hibridmatthias@sbcglobal.net] # I have tried while loops but cant get it to work as well as # making it into a method. Is there a different way to do this # that my lack of experience is preventingme from seeing/ Is # iteration over a an array something worth following? I # apologize for such a basic question, but the PickAxe,the book # by Chris Pine, and multiple web sites are failing me and I am # turnig here as a last resort... the pickaxe and pine's book are good enough. one only needs practice, think like exercise or kata :) if a lot if/else boggles your mind (maybe because of deciding when to loop back and what to print), you might want to delegate the sex choice to a list checker, say like "check entry from a list. do foo if not found otherwise do blah..."; thus letting ruby do the search logic for us and we only take care of the loop problem ... eg, botp@pc4all:~$ cat test.rb #------------------------------------ sexCheckH = Hash.new sexCheckH = { "m" => "male", "f" => "female" } doseSexS = nil until doseSexS do puts 'What gender is your patient? Please enter m or f' doseSexS = sexCheckH[ptSexS = gets.chomp.downcase] puts "Invalid entry [#{ptSexS}]. Pls retry" unless doseSexS end puts "Your patient is a #{doseSexS}." #------------------------------------ botp@pc4all:~$ ruby test.rb What gender is your patient? Please enter m or f x Invalid entry [x]. Pls retry What gender is your patient? Please enter m or f m Your patient is a male. botp@pc4all:~$ ruby test.rb What gender is your patient? Please enter m or f y Invalid entry [y]. Pls retry What gender is your patient? Please enter m or f f Your patient is a female. botp@pc4all:~$ kind regards -botp