Compare User Input With Values
This is a way to continuously prompt a user for input until they select a pre-determined, valid choice from an array. It could also be modified to test for a value in a database.
This is a way to continuously prompt a user for input until they select a pre-determined, valid choice from an array. It could also be modified to test for a value in a database.
class Choices
class Config
@@actions = ['list', 'find', 'add', 'quit']
def self.actions; @@actions; end
end
def get_action
# Keep asking for user input until we get a valid action
action = nil
until Choices::Config.actions.include?(action)
puts "Actions: " + Choices::Config.actions.join(", ")
print "> "
user_response = gets.chomp
args = user_response.downcase.strip.split(' ')
action = args.shift #first word is action, remaining words are args
end
return action, args
end
end