t0code
t0code

Reputation: 21

Looping a check user input for matching string?

def get_input(*options)
  puts "Choose one:"
  options.each do |option|
    puts "#{option}"
  end
  choice = gets.chomp.to_s
  options.each do |option|
    if choice.include?(option.to_s)
    return choice
    end
  end
  puts "Command not found..."
  get_input(options)
end

if a user enters an invalid command it puts the available commands again but with the current value including [,]. How would I avoid this.

Upvotes: 2

Views: 33

Answers (1)

Félix Saparelli
Félix Saparelli

Reputation: 8719

You're missing a splat in the recursive call for get_input:

  puts "Command not found..."
  get_input(options)
end

should be:

  puts "Command not found..."
  get_input(*options)
end

Upvotes: 1

Related Questions