module PutsUtils::PutsPrompt

Public Instance Methods

puts_prompt(question, required: false, default: nil) click to toggle source

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength

# File lib/puts_utils/puts_prompt.rb, line 7
def puts_prompt(question, required: false, default: nil)
  tries = 1
  loop do
    STDOUT.print question
    answer = STDIN.gets.strip
    return answer if answer.present?
    return default if answer.empty? && required == false
    raise 'Value is required' if answer.empty? && required && tries == 3

    puts 'Sorry, this is required – please try again'
    tries += 1
    next
  end
end