Module: EverydayCliUtils::Ask
- Defined in:
- lib/everyday-cli-utils/ask.rb
Class Method Summary collapse
- .ask(question, options, &block) ⇒ Object
- .ask_yn(question, &block) ⇒ Object
- .hash_to_options(hash, extra = []) ⇒ Object
- .setup_options(options) ⇒ Object
Class Method Details
.ask(question, options, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/everyday-cli-utils/ask.rb', line 13 def self::ask(question, , &block) val = '-1' while true print "#{question}\n\n#{()}\n\n" val = Readline.readline("Please enter your choice (1 - #{.count}): ", true) if !(val =~ /^\d+$/).nil? && (1...count).include?(val.to_i) break end print "\n\nYou must enter an integer between 1 and #{.count}. Please try again.\n\n" end block.call([val.to_i - 1]) end |
.ask_yn(question, &block) ⇒ Object
26 27 28 29 |
# File 'lib/everyday-cli-utils/ask.rb', line 26 def self::ask_yn(question, &block) resp = Readline.readline("#{question} (yes/no) ", true) block.call(resp.downcase == 'yes' || resp.downcase == 'y') end |
.hash_to_options(hash, extra = []) ⇒ Object
31 32 33 |
# File 'lib/everyday-cli-utils/ask.rb', line 31 def self::(hash, extra = []) hash.keys + extra end |
.setup_options(options) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/everyday-cli-utils/ask.rb', line 5 def self::() mapped = [] .each_with_index { |v, k| mapped << "#{k+1}) #{v.to_s}" } mapped.join("\n") end |