Skip to content

Instantly share code, notes, and snippets.

@adparadise
Created December 8, 2011 04:59
Show Gist options
  • Save adparadise/1446168 to your computer and use it in GitHub Desktop.
Save adparadise/1446168 to your computer and use it in GitHub Desktop.
Simple class to prompt a user on the command line for a response.
class UserInput
# Prompt for confirmation, exiting if not an affirmative.
def self.confirm!(message)
unless confirm(message)
STDERR.write("cancelling...\n")
exit
end
end
# Prompt for confirmation, returning a boolean indicating user's preference.
def self.confirm(message)
response = prompt(message + "\ny / N: ").strip.downcase
response == "y"
end
# Prompt the user and await a response.
def self.prompt(message)
STDERR.write(message)
STDIN.gets
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment