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