2

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?

bright-star
  • 6,016
  • 6
  • 42
  • 81
  • The loop that you posted above has two `break` options... – Nir Alfasi Jan 17 '14 at 21:05
  • 1
    Just a heads-up: this may offend lexicographers who believe 'dumbass' should be hyphenated. `'I guess you aren\'t cool enough.'` is normally written `"I guess you ain't cool enough"`. – Cary Swoveland Jan 17 '14 at 21:26

2 Answers2

3

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

Community
  • 1
  • 1
MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123
  • This solution avoids the repetition of the literals _yes_ and _no_ in the code in question. That's good. – Wayne Conrad Jan 17 '14 at 21:40
  • Wayne, the loop structure you've used is often seen when the user typically performs several actions before selecting "quit". Here you are using it just to get a valid answer. It might be clearer to have something like `loop do; puts 'Do you have a middle name?'; break if['yes', 'no'].include(answer=gets.chomp.downcase); print "So I'll ask again "; end`, then `if answer == 'yes'...else...end`. (SO: I wish you'd allow better formatting in comments.) – Cary Swoveland Jan 17 '14 at 22:11
  • @Cary, I'm not the author of the code you are commenting on. However, I agree that it's usually better to ask the question separately from the code the acts upon the answer. – Wayne Conrad Jan 17 '14 at 23:18
0

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.

pjs
  • 18,696
  • 4
  • 27
  • 56