Module: Executable::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/executable/utils.rb

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

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, options={})
  print "#{question} [default: #{options[:default]}] "
  reply = STDIN.readline.chomp
  if reply.empty?
    options[:default]
  else
    reply
  end
end

#no?(question, options = {}) ⇒ Boolean

Query the user for a yes/no answer, defaulting to no.

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/executable/utils.rb', line 22

def no?(question, options={})
  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.

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/executable/utils.rb', line 15

def yes?(question, options={})
  print "#{question} [Y/n] "
  input  = STDIN.readline.chomp.downcase
  BOOLEAN_MAP[input] || true
end