If the user doesn't response with anything, multiple times


#1

If you asked the user multiple questions and wanted to replace all instances of "s" with "th", could you write an umbrella if statement that would supply a set response to all questions if any of them were given a blank response.

As in, rather than put some version of an if empty statement after each question, can you include an if statement before or after all questions that says, if any questions is not answered, prompt with "some statement".


#2

I think what you want is a behaviour like this:

If question1 is empty OR question2 is empty OR question3 is empty
print "generic statement"

Did I understand correctly?


#3

Yes thank you so much! I really appreciate it.

So I would replace question1/question2/questions with the variable name I assign the .gets.chop to.

For example
puts "what is your name?"
name = gets.chop
puts "what is your age?"
age = gets.chop

if age is empty OR name is empty
print "I didn't get that, please answer the question"


If that's correct, does that go before or after the questions are asked? I'm confused by how ruby processes code, from the top down or the bottom up.

I know in Javascript I would normally say something like

If (name = ' ' II name = null II !isNaN (name))
Alert('some statement);
)

I'm typing on a phone so excuse the improper punctuation. I'm trying to find the equivalent way to check for all of those things, and if possible how to do it for all questions at once.


#4

.chomp*
Sorry autocorrect


#5

Ruby works just like any interpreted language, from top to bottom.

So put your condition check after your input.

Something like:

if age.nil? || name.nil?
*do stuff*
end

However, if you want to keep asking for input as long as these conditions are met, you'll need to put all this in a loop of course.

EDIT: if the variables you're testing are getting input from the user, the .nil? method will likely not work because you'll still get an empty string object ("") and not a nil object. Compare them to "" instead.


#6

This is how i did. Seems to work just fine with the "while" method.

print "Thring Pleath: "
user_input = gets.chomp

while user_input.empty?
print "Thring Pleath: "
user_input = gets.chomp
end

if user_input.include? ("s" || "S") || ("c" || "C")
user_input.gsub!(/s/, "th")
user_input.gsub!(/S/, "Th")
user_input.gsub!(/c/, "th")
user_input.gsub!(/C/, "Th")
puts "Daffy says: #{user_input}"

else
puts "Nothin to thee here."
end