0

I'm in the early stages in learning Ruby and ran into an issue when trying to create a conditional statement for one of my programs. I basically wanted it to loop back and grab a fresh input value if the orignal didn't meet the criteria of the first two conditionals.

So for example:

puts "Choose either the 'red' or 'blue' pill"

choice = gets.to_s.downcase.chomp

if choice == red
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
elsif choice == "blue"
   puts "The story ends, you wake up in your bed and believe whatever you want to believe"
else
    puts "You have to choose one"
end
1
  • Is red a variable? If not, choice == red will fail. It's important your source code be runnable. Commented Feb 21, 2014 at 18:59

4 Answers 4

3

Here's another common construct:

loop do
  puts "Choose either an 'upper' or a 'downer'"

  case gets.downcase.chomp
  when "upper"
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye"
    break 
  when "downer"
    puts "The story ends, you wake up and believe whatever you want to believe"
    break
  else
    puts "You have to choose one"
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Nice use-case of using #loop here. But gets.to_s.downcase.chomp can be written as gets.downcase.chomp.
Thanks, @Arup, I just blindly cut-and-pasted. Shame on me! Will fix.
1
begin
  puts "Choose either the 'red' or 'blue' pill"
  choice = gets.to_s.downcase.chomp

  if choice == "red"
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
  elsif choice == "blue"
    puts "The story ends, you wake up in your bed and believe whatever you want to believe"
  else
    puts "You have to choose one"
    choice = "invalid"
  end
end while(choice == "invalid")

console output:

Choose either the 'red' or 'blue' pill
#empty input
You have to choose one
Choose either the 'red' or 'blue' pill
red
Fasten your seatbelt dorothy 'cause kansas is going bye-bye
 => nil 

5 Comments

There is no such method as String#blank?. Even if there was, this doesn't work right.
blank? is a rails thing.
@DGM yes, blank? is rails. I changed it.
I see what you did. What if I said "green"? Having the user input a value not listed is what I was looking for.
add choice = nil under else statement and change choice.empty? to choice.
1

How about Ruby's throw and catch for this scenario :

def ask
  puts "Choose either the 'red' or 'blue' pill"
  choice = gets.downcase.chomp
  if choice == 'red'
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
  elsif choice == "blue"
    puts "The story ends, you wake up in your bed and believe whatever you want to believe"
  else
    puts "You have to choose one"
    throw :done,ask
  end
end

catch(:done) do
  ask
end

Lets run the code :

(arup~>Ruby)$ ruby -v a.rb
ruby 2.0.0p0 (2013-02-24 revision 39474) [i686-linux]
Choose either the 'red' or 'blue' pill
foo
You have to choose one
Choose either the 'red' or 'blue' pill
bar
You have to choose one
Choose either the 'red' or 'blue' pill
blue
The story ends, you wake up in your bed and believe whatever you want to believe
(arup~>Ruby)$ 

Comments

0
class WrongChoice < StandardError
end

puts "Choose either the 'red' or 'blue' pill"

begin
  choice = gets.to_s.downcase.chomp
  raise WrongChoice.new unless ['blue', 'red'].include? choice 
  if choice == red
    puts "Fasten your seatbelt dorothy 'cause kansas is going bye-bye" 
  else 
   puts "The story ends, you wake up in your bed and believe whatever you want to"
  end
rescue WrongChoice
  puts "You have to choose one"
  retry
end

1 Comment

While I wouldn't use an exception for this, the retry is very elegant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.