Accept multiple input possibilities for a single conditional branch - ruby

I have this code:
choice = $stdin.gets.chomp
if choice == "yes"
puts "ok sure"
else
puts "try again"
end
I have more than one possibility of user input to put "ok sure". That is, if I input "ok", it should still say "ok sure".

Use case construction.
case choice
when "yes", "ok"
puts "ok sure"
else
puts "try again"
end

Easiest way would be to use Array#include? like so:
choice = $stdin.gets.chomp
if ["yes", "ok"].include? choice
puts "ok sure"
else
puts "try again"
end

Related

Ruby - if else statement

I have been working on the configuration an else statement.
Here is the code:
puts "Do you like PC?"
case (gets.chomp)
when 'yes'
puts "I do too"
when 'no'
puts "Mac is better"
end
Now I am trying to configure it to (1) use else, and (2) to use elsif. How do I better write this to use "else", and how do I write it to use elsif. Here is what I wrote for the else version
puts "Do you like PC?"
case (gets.chomp)
if 'yes'
puts "I do too"
else
puts "Mac is better"
end
You should re-write as:
puts "Do you like PC?"
answer = gets.chomp
if answer == 'yes'
puts "I do too"
elsif answer == 'maybe'
puts "you're confused"
else
puts "Mac is better"
end
The keyword case is preserved for case statements.

ruby show #{} when nothing is entered

print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... "
calculate_cont = ""
while calculate_cont = gets.chomp()
calculate_cont.to_s
calculate_cont.upcase!
case calculate_cont
when "Y", "YES"
<calculations>
print "Do you wish to continue? "
when "N", "NO"
puts "Thank you! Press Enter to exit!"
gets()
exit()
else
puts "Error: #{calculate_cont} is not a valid entry!
puts "Please Enter a valid response! Yes(Y) or No(N)"
print "Do you wish to continue? "
end
end
when the user enter anything else other than YES or NO it will show the text that he input using #{calculate_cont}. How can i tell the user that he enters "Nothing" if the user just hit enter using #{calculate_cont}? can it be done?
Thanks
Yes, if you are calling gets.chomp and the user entered no value, the output will be an empty string. Just put another conditional
DO SOMETHING if calculate_cont.empty?
i created this method:
methods
def output(calculate_cont)
if calculate_cont == ""
return "Nothing"
else
return calculate_cont
end
end
with this output..
print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... "
calculate_cont = ""
while calculate_cont = gets.chomp()
calculate_cont.to_s
calculate_cont.upcase!
case calculate_cont
when "Y", "YES"
<calculations>
print "Do you wish to continue? "
when "N", "NO"
puts "Thank you! Press Enter to exit!"
gets()
exit()
else
system("cls")
puts ""
puts "Error: #{output(calculate_cont)} is not a valid entry!"
puts "Please Enter a valid response! Yes(Y) or No(N)"
print "Do you wish to continue? "
end
end
this way i can show the user what he typed. :) many thanks

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

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.

Is there a a "back to the top" in Ruby? [duplicate]

This question already has answers here:
Is there goto statement in Ruby?
(4 answers)
Closed 8 years ago.
I'm kind of noob in ruby programming. My question is if there's something like anchors or goto in ruby, so I can create loops inside Ifs... Is there something like that?
Example:
anchorX
gets variable
if variable == "option one"
puts "you choose right"
else
puts "you choose wrong! DO IT AGAIN!"
go to anchorX
No, Ruby does not have goto. Try a loop instead:
loop {
input = gets.chomp
if input == "option one"
puts "you choose right"
break
else
puts "you choose wrong! DO IT AGAIN!"
end
}
Or, an alternative method (and arguably slightly more readable):
input = gets.chomp
until input == "option one"
puts "you choose wrong! DO IT AGAIN!"
input = gets.chomp
end
puts "you choose right"
Why would you want to use goto for something so trivial?
Use a while loop. The while syntax for ruby is:
while [condition] do
(put your code here)
end
For example:
gets variable
while variable != "option one" do
puts "you choose wrong!"
gets variable
puts "you choose right"
Besides loop statements, you can use retry statement to repeat entire begin/end block.
begin
input = gets.chomp
if input == "option one"
puts "you choose right"
else
puts "you choose wrong! DO IT AGAIN!"
raise
end
rescue
retry
end

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

Resources