From: Bertram Scharpf Date: 2009-12-21T03:56:51+09:00 Subject: Re: simple one Hi, Am Sonntag, 20. Dez 2009, 03:20:33 +0900 schrieb beny 18241: > i used both methods and they works (thanks for that), but i have ine > more question (both methods are affected) > > --- > #!/usr/bin/ruby > > def ask(prompt) > loop do > print prompt, ' ' > $stdout.flush > s = gets > exit if s == nil > s.chomp! > if s == 'y' or s == 'yes' > return true > elsif s == 'n' or s == 'no' > return false > else > $stderr.puts "Please answer yes or no." > end > end > end > > ask(' => ') > --- > > i Saved it as a file.rb, as there is a small part of my bigger scirpt , > i wanted to add argument, so i'am running it with argument(which is used > later in my script): > > ./file.rb file.xml > > and i recive > => Please answer yes or no. > => Please answer yes or no. > => Please answer yes or no. > ... > > I know that I should clear input or something like that, but i dont know > how , I' ve tried many ways. There must be a loop around the `ask' method. By the way, why don't you use `case'? def ask prompt loop do print prompt, ' ' $stdout.flush s = gets or break s.chomp! case s when /^y/i, "ja", "oui" then break true when /^n/i then break false else $stderr.puts "Please answer yes or no." end end end ask ' =>' Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany * Discover String#notempty? at .