How to wrap a loop around an if statement [duplicate] - ruby

This question already has answers here:
Is there a "do ... while" loop in Ruby?
(10 answers)
Closed 8 years ago.
I'm coding a program that asks whether or not the user wants to give his name. If the user responds 'yes', the question is asked; on 'no' the program quits. If the users enter anything else, they are reminded to say either 'yes' or 'no'.
My code so far:
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
if answer == "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
elsif answer == "no"
puts "Oh, ok. Good bye"
else
puts "You need to answer yes or no"
end
I need start over, if the user does not enter 'yes' or 'no' for the initial question.

You can solve that problem with a while loop, that breaks only when the correct input is made.
puts "Would you like to give us your name? (type yes or no)"
while answer = gets.chomp
case answer
when "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
break
when "no"
puts "Oh, ok. Good bye"
break
else
puts "You need to answer yes or no"
end
end

answer = ""
while (answer != "yes" && answer != "no") do
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
end
if answer == "yes"
print "What's your name?"
name = gets.chomp
puts "Nice to meet you, #{name}"
elsif answer == "no"
puts "Oh, ok. Good bye"
else
puts "You need to answer yes or no"
end

This would be better accomplished creating a Method
Something like this will work for you:
def getname
# ask the user if we should continue
puts "Would you like to give us your name? (type yes or no)"
answer = gets.chomp
if answer == "yes"
# the user said yes. get the name
print "What's your name?"
name = gets.chomp
elsif answer == "no"
# the user said no. get out of here
puts "Oh, ok. Good bye"
else
# the user didnt answer correctly
puts "You need to answer yest or no"
# so we call this very function again
getname
end
end
# call the above method that we created
getname
What we did here was wrap your code in a method declaration. In that very method declaration we call that very method if the user doesnt supply the expected input.
Hope this helps.

Related

Ruby Endless Loop

I am new to Ruby. I have written the code below, its working about 90% except the else statement. The else statement triggers endless loop. I just want it to ask user to try again. Here is my code
puts "Do you want to print something? (Y / N)"
user = gets.chomp.downcase
answer = true
while answer
if user == "y"
puts "Something"
answer = false
elsif user == "n"
puts " "
answer = false
else
puts "Invalid input! Please enter Y or N"
end
end
Somewhat shorter (note user has gone, the answer is now referred to as answer).
answer = ""
until (answer == "y") or (answer == "n")
puts 'Do you want to print something? (Y/N)'
answer = gets.chomp.downcase
end
Once you exit the else, answer is still true. If you want to re-prompt, you can move your puts and user statement into the loop.
Something like this should work.
while true # (alternately) loop do
puts 'Do you want to print something? (Y/N)'
case gets.chomp.downcase
when 'y'
puts 'foo'
break
when 'n'
puts 'bar'
break
else
puts 'Invalid input! Please enter Y or N'
end
end
You can use break to exit out of your loop instead of setting up another variable. Also, this looks like a good use-case for a case statement to have some explicit cases listed.

Enter with no input as a valid boolean

I'm writing some very simple code, asking for confirmation on a text input, and
what I want to do is that if the users simply presses "Enter", make it count as a "yes". For example:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || ''
other_method
else
method
end
end
But when I run it on Ruby, I get the "Warning, literal string in condition", and whatever you enter, calls the "other_method". The solution I found is the following:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y'
other_method
elsif correct == ''
other_method
else
method
end
end
But it's pretty annoying, I'd rather understand why the first one doesn't work, and how can I make it work using the | |
Thank you!
What the error is saying is that you are supplying a string (literal) inside of a conditional statement by itself. When you do if correct == "y" || "" you're actually telling it if correct == "y" OR "" and just supplying the string by itself is not a condition.
To fix this you'd simply supply the condition after the operator as well as before it. Ruby does not assume you want the same thing to happen after the ||.
Like this:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || correct == ''
other_method
else
method
end
end
Hope this helps. Happy coding
The solution here is to use Ruby's very versatile case statement to set up a number of "cases" you want to test:
puts "you entered '#{#text}', is it correct?"
case (gets.chomp)
when 'y', 'yes', ''
method_a
else
method_b
end
This can be extended to use regular expressions for even more versatility:
case (gets.chomp)
when /\A\s*y(?:es)?\s*\z/i
method_a
else
method_b
end
Where now anything like "y" or "yes" or "Yes " will work.
When you have bunch of if statements all testing the same variable, consider using a case statement to simplify your logic.
Here is another option using Regex (Docs):
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if /^y?$/ =~ correct # This will match 'y' and empty string both
other_method
else
method
end

Ruby trouble with breaking loop

All right, I have the programming aptitude of a goldfish, so I could use some help. I have the following code (please excuse my terrible sense of humor):
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
while true
if answer != ('yes' || 'no')
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
elsif answer == ('yes' || 'no')
if answer == 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
break
if answer == 'no'
puts 'I guess you aren\'t cool enough.'
break
end
end
end
end
puts 'Well, smell ya later.'
It works mostly fine, but I have one problem: choosing the no option. I cannot figure out how to get that to work. It will loop fine, and choosing the yes option works.
Basically, my question is: how do I create a loop with two break options?
For something like this you should use a case/when which is a Ruby switch statement because having all of those if/end blocks will get confusing fast.
Also please read this guide: https://github.com/bbatsov/ruby-style-guide
It will teach you how to properly format your ruby code.
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
case answer
when 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
when 'no'
puts 'I guess you aren\'t cool enough.'
else
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
end
puts 'Well, smell ya later.'
And if you always want it to loop when they don't answer yes or no. You can do that by wrapping the code block in a loop as follows:
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
loop do
case answer
when 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
break
when 'no'
puts 'I guess you aren\'t cool enough.'
break
else
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
end
end
puts 'Well, smell ya later.'
See this answer: How to write a switch statement in Ruby
if answer != ('yes' || 'no') isn't doing what you think it is. Since 'yes' is non-nil, which is true, the logical OR is short-circuited and the value of the parenthetic expression is always 'yes'. Try if answer != 'yes' && answer != 'no' instead.

Variables in if/else statement won't work

I'm creating an interactive story, not a game. The options don't work in the if/else; here is the code.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if #{choice}==1
puts "you chose yes"
elsif #{choice}==2
puts "you chose no"
else
puts "Invalid Choice"
I tried leaving it intro, but that just calls the else statement, and with this setup, the tic tac toe guy in front of the brackets, it always calls the first option. please help.
if #{choice}==1 isn't how you test the value of variables. #{} is for string interpolation; you don't have to interpolate anything. Just use if choice == "1". Note that, because you're reading a string from the console, you need to compare against "1", not 1.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if choice == "1"
puts "you chose yes"
elsif choice == "2"
puts "you chose no"
else
puts "Invalid Choice"
end

How do I loop a request for user input until the user enters the correct info?

I am a beginner who is trying to learn Ruby. I have learned some of the easier stuff so far, but I seem to be stuck in trying to combine a couple of things I've learned.
What I am trying to do is to ask the user a question and tell them to enter either 1 or 2. A simple if statement would let me respond with one option if they enter 1, and another option if they enter 2. However, if they enter something entirely different like a different number, a string, etc., how can I prompt them to try again and have it loop back to the original question?
What I have so far looks something like this.
prompt = "> "
puts "Question asking for 1 or 2."
print prompt
user_input = gets.chomp.to_i
if user_input == 1
puts "One response."
elsif user_input == 2
puts "Second response."
else
puts "Please enter a 1 or a 2."
end
This is where I'm stuck. How do I make it go back to the "Question asking for 1 or 2." until the user enters a 1 or 2? I know it's probably a loop of some kind, but I can't seem to figure out which kind to use and how to incorporate asking for user input repeatedly (if necessary) until getting the desired input. Any help would be greatly appreciated. Thanks.
You're right that you need to run your code in a loop. Using a while loop with gets.chomp as a condition, you can carry on asking for user input until you decide you've got what you want.
In this case, you want to validate the answer to the question and ask again if it's invalid. You don't need to change a great deal, except making sure you break out of the loop when the answer is correct. If the answer is wrong, print the prompt again.
This is a slightly refactored version that uses case instead, but it shows what you need to do. There is no doubt a cleaner way to do this...
prompt = "> "
puts "Question asking for 1 or 2."
print prompt
while user_input = gets.chomp # loop while getting user input
case user_input
when "1"
puts "First response"
break # make sure to break so you don't ask again
when "2"
puts "Second response"
break # and again
else
puts "Please select either 1 or 2"
print prompt # print the prompt, so the user knows to re-enter input
end
end
Try using the until method like this:
prompt = "> "
print prompt
user_input = nil
until (user_input == 1 or user_input == 2)
puts "Please enter a 1 or 2."
user_input = gets.chomp.to_i
end
if user_input == 1
puts "One response."
elsif user_input == 2
puts "Second response."
else
puts "Please enter a 1 or a 2."
end
user_input = 0
until [1,2].include? user_input do
puts "Please enter a 1 or a 2.>"
user_input = gets.chomp.to_i
end
if user_input == 1
puts "One response."
else
puts "Second response."
end
You can try this to make your code clean.
While the title of this question is somewhat unrelated, please see the trick that is used here: Ruby Retry and ensure block is not working
The use of error detection and unique retry keyword available in Ruby allows you to easily do a retry-loop compacted together with nice an error handling.
However, mind that the example I pointed is not really the best. There are some minor issues. For example, you should not catch Exception, rather simple rescue => e would be enough. But the overall idea should be rather clear.

Resources