Module: Executable::Utils
Overview
Some handy-dandy CLI utility methods.
Constant Summary collapse
- BOOLEAN_MAP =
Strings to interprest as boolean values.
{"y"=>true, "yes"=>true, "n"=>false, "no"=>false}
Instance Method Summary collapse
-
#ask(question, options = {}) ⇒ Object
Query the user for an answer.
-
#no?(question, options = {}) ⇒ Boolean
Query the user for a yes/no answer, defaulting to no.
-
#yes?(question, options = {}) ⇒ Boolean
Query the user for a yes/no answer, defaulting to yes.
Instance Method Details
#ask(question, options = {}) ⇒ Object
Query the user for an answer.
29 30 31 32 33 34 35 36 37 |
# File 'lib/executable/utils.rb', line 29 def ask(question, ={}) print "#{question} [default: #{[:default]}] " reply = STDIN.readline.chomp if reply.empty? [:default] else reply end end |
#no?(question, options = {}) ⇒ Boolean
Query the user for a yes/no answer, defaulting to no.
22 23 24 25 26 |
# File 'lib/executable/utils.rb', line 22 def no?(question, ={}) print "#{question} [y/N] " input = STDIN.readline.chomp.downcase BOOLEAN_MAP[input] || false end |
#yes?(question, options = {}) ⇒ Boolean
Query the user for a yes/no answer, defaulting to yes.
15 16 17 18 19 |
# File 'lib/executable/utils.rb', line 15 def yes?(question, ={}) print "#{question} [Y/n] " input = STDIN.readline.chomp.downcase BOOLEAN_MAP[input] || true end |