From: Dark Ambient Date: 2006-06-27T00:20:00+09:00 Subject: Re: Stumped:beginners question Well, now the author has introduced us to the wonderful world of recursion. Removes the need for the loop. I imagine very useful in various situations. Stuart On 6/25/06, dblack@wobblini.net wrote: > Hi -- > > On Mon, 26 Jun 2006, Cliff Cyphers wrote: > > > On Mon, Jun 26, 2006 at 08:57:05AM +0900, Pete Yandell wrote: > >> I'm just going to rewrite this, because there are a bunch of bad > >> things in it. > >> > >> def ask(question) > >> response_is_valid = false > >> result = false > >> while !response_is_valid > >> puts question > >> reply = gets.chomp.downcase > >> case reply > >> when 'yes' > >> result = true > >> response_is_valid = true > >> when 'no' > >> result = false > >> response_is_valid = false > >> else > >> puts 'Please answer "yes" or "no".' > >> end > >> end > >> result > >> end > >> > > > > much simpler than using the case statement: > > > > def ask(question) > > puts question > > reply = gets.strip.downcase > > while ("#{reply}" != "yes") && ("#{reply}" != "no") > > You're working too hard :-) No need to do string interpolation: > > while (reply != "yes") && (reply != "no") > > or even: > > until %w{ yes no }.include?(reply) > > > David > > -- > David A. Black (dblack@wobblini.net) > Ruby Power and Light, LLC (http://www.rubypowerandlight.com) > > See what the readers are saying about "Ruby for Rails"! > http://www.rubypowerandlight.com/quotes > >