Module: Questions

Defined in:
lib/cli_questions.rb,
lib/cli_questions/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.multichoice(question, valid_options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cli_questions.rb', line 26

def self.multichoice(question, valid_options)
  while true do
    puts question
    valid_options.each_with_index do | option, index |
      puts "#{index + 1} : #{option}"
    end
    user_answer = $stdin.gets.chomp.to_i - 1
    return valid_options[user_answer] if user_answer > -1 && user_answer < valid_options.count
    puts "INVALID OPTION"
  end
end

.pick_a_number(question, valid_numbers) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cli_questions.rb', line 11

def self.pick_a_number(question, valid_numbers)
  while true do
    puts question
    print "( "
    valid_numbers.each_with_index do | number, index |
      print "#{number}"
      print ", " unless index == valid_numbers.count - 1
    end
    puts " )"
    user_answer = $stdin.gets.chomp.to_i
    return user_answer if valid_numbers.include?(user_answer)
    puts "INVALID OPTION"
  end
end

.testObject

Your code goes here…



7
8
9
# File 'lib/cli_questions.rb', line 7

def self.test
  true
end

.yes_or_no?(question) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/cli_questions.rb', line 38

def self.yes_or_no?(question)
  while true do
    $stdout.puts question
    $stdout.puts "1. Yes"
    $stdout.puts "2. No"
    input = $stdin.gets.chomp.to_i
    return 1 == input if input == 1 || input == 2
  end
end