bigpotato
bigpotato

Reputation: 27547

Is there a "while...else" in Ruby?

I have a hash with cat names for keys and cat instances for values. Is there a way to make it so that when people type a response that isn't in a key in the cats hash for the terminal to reprint the puts "Which cat would you like to know about?" question or type: "Try again"? I guess I'm asking for sort of a "while... else".

puts "Which cat would you like to know about?"
puts cats.keys
response = gets.chomp

while cats.include?(response)
  puts "The cat you chose is #{cats[response].age} old"
  puts "The cat you chose is named #{cats[response].name}"
  puts "The cat you chose is a #{cats[response].breed} cat"
  puts "Is there another cat would you like to know about?"
  response = gets.chomp
end

Upvotes: 1

Views: 7854

Answers (4)

skunkfrukt
skunkfrukt

Reputation: 1570

There isn't a "while...else", as far as I know. If you do not mind the loop continuing regardless of whether the response is a valid cat name or not, maybe this will work for you:

puts "Which cat would you like to know about?"
puts cats.keys

while true
  response = gets.chomp
  if response.empty?
    break
  elsif cats.include?(response)
    puts "The cat you chose is #{cats[response].age} old"
    puts "The cat you chose is named #{cats[response].name}"
    puts "The cat you chose is a #{cats[response].breed} cat"
    puts "Is there another cat would you like to know about?"
  else
    puts "There is no cat with that name. Try again."
  end
end

This will repeatedly prompt the user for a cat name, until the user responds with an empty string, at which point it will break out of the loop.

Upvotes: 2

sawa
sawa

Reputation: 168239

continuous = false
loop do
  unless continuous
    puts "Which cat would you like to know about?", cats.keys
  else
    puts "Is there another cat would you like to know about?"
  end
  if cat = cats[gets.chomp]
    puts "The cat you chose is #{cat.age} old"
    puts "The cat you chose is named #{cat.name}"
    puts "The cat you chose is a #{cat.breed} cat"
    continuous = true
  else
    continuous = false
  end
end

Upvotes: 0

Derek
Derek

Reputation: 1114

It has been a while since I have played with Ruby, but something like this comes to mind:

def main
   print_header
       while true
           resp = get_response
           if cats.include?(resp)
              print_info(resp)
           else
              print_header
       end
end

def get_response
    puts cats.keys
    response = gets.chomp
end

def print_header
    puts "Which cat would you like to know about?"
end

def print_info response
  puts "The cat you chose is #{cats[response].age} old"
  puts "The cat you chose is named #{cats[response].name}"
  puts "The cat you chose is a #{cats[response].breed} cat"
  puts "Is there another cat would you like to know about?"
end

Please note that you will need a terminal point. If get_response returns "no", then quit.

Upvotes: 0

knut
knut

Reputation: 27875

You could rearrange your code with an additional question:

cats = {'a' => 1} #testdata
continue = true   #set a flag

while continue
  puts "Which cat would you like to know about?"
  response = gets.chomp
  while cats.include?(response)
    puts cats[response]
    puts "Is there another cat would you like to know about?"
    response = gets.chomp
  end
  puts "Try another? (Y for yes)"
  continue = gets.chomp =~ /[YyJj]/ #Test for Yes or yes, J for German J...
end

Upvotes: 0

Related Questions