From: Robert Klemme Date: 2007-12-16T21:55:00+09:00 Subject: Re: How t owrite an OR statement On 16.12.2007 07:18, Daniel Peikes wrote: > Joseph Pecoraro wrote: >>> while again != "y" || "n" >> >> These don't use the OR logic, but they are similiar. >> until again ~= /^[yn]/i do >> ... >> end >> >> Or if your wanted to keep the while keyword you could do this as: >> while again[0] != /^[^yn]/i do >> ... >> end >> >> >> I don't know if you can do exactly what you are trying to do with that >> syntax. You can reconstruct the logic like this: >> while again != "y" && again != "n" do >> ... >> end >> >> >> Here is another approach more along the lines of what you have (is >> something in a list of something else), likewise the until loop could be >> made a while loop: >> good_chars = %w(y n) #=> ['y', 'n'] >> until good_chars.include? again do >> ... >> end > > This is what I currently have, but it looks to be giving me an infinite > loop: > again = "y" > number = [] > > while again == "y" > > > puts "Please enter a number." > > number = gets.chomp > > puts "Would you like to enter a number again? y/n" > > again = gets.chomp.downcase > > while again != "y" or "n" > > puts "Would you like to enter a number again? y/n" > > end > > end > > puts number > > hold = gets Your logic will become easier if you use post checked loops: numbers = [] again = nil begin puts "Please enter a number." numbers << Integer(gets) begin puts "Would you like to enter a number again? y/n" again = gets.chomp.downcase end until again == "y" || again == "n" end while again == "y" puts numbers With regard to "or": "or" has a much lower precedence than "||" so it is usually better in conditions like these to use "||". Kind regards robert