class GemHadar

def ask?(prompt, pattern, default: nil)

Returns:
  • (MatchData, nil) - the result of the pattern match or nil if no match

Parameters:
  • pattern (Regexp) -- the regular expression to match against the input
  • prompt (String) -- the message to display to the user
def ask?(prompt, pattern, default: nil)
  if prompt.include?('%{default}')
    if default.present?
      prompt = prompt % { default: ", default is #{default.inspect}" }
    else
      prompt = prompt % { default: '' }
    end
  end
  STDOUT.print prompt
  answer = STDIN.gets.chomp
  default.present? && answer.blank? and answer = default
  if answer =~ pattern
    $~
  end
end