Created
December 8, 2011 04:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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