Class: Question
- Inherits:
-
Object
- Object
- Question
- Defined in:
- lib/utils/question.rb
Overview
The Question class facilitates the creation and management of interactive questions in the console. It provides methods to validate and return user input as various data types including boolean, float, integer, options (from a list), and string.
Instance Method Summary collapse
-
#bool_answer(error_message: nil) ⇒ Boolean
Prompts the user with a boolean question and returns the answer as true or false.
-
#float_answer(error_message: nil) ⇒ Float
Prompts the user for a floating-point number and returns the value.
-
#initialize(message) ⇒ Question
constructor
Initializes a new instance of the Question class.
-
#integer_answer(error_message: nil) ⇒ Integer
Prompts the user for an integer and returns the value.
-
#option_answer(options, error_message: nil) ⇒ Object
Prompts the user to select an option from a given list and returns the selected option.
-
#string_answer(regex: nil, error_message: nil) ⇒ String
Prompts the user for a string that matches a given regular expression.
Constructor Details
Instance Method Details
#bool_answer(error_message: nil) ⇒ Boolean
Prompts the user with a boolean question and returns the answer as true or false.
21 22 23 24 25 26 27 28 29 |
# File 'lib/utils/question.rb', line 21 def bool_answer(error_message: nil) error = || File.join('question', 'error', 'bool') bool_sufix = Message.new(File.join('question', 'bool_sufix')) result = read_input("#{@message} #{bool_sufix}", error_message: error) do |input| input.match?(/^((Y|y)((E|e)(S|s))*)|((N|n)(O|o)*)$/) end result.match?(/^(Y|y)((E|e)(S|s))*$/) end |
#float_answer(error_message: nil) ⇒ Float
Prompts the user for a floating-point number and returns the value.
35 36 37 38 |
# File 'lib/utils/question.rb', line 35 def float_answer(error_message: nil) error = || File.join('question', 'error', 'float') string_answer(regex: /^\d+\.\d+$/, error_message: error).to_f end |
#integer_answer(error_message: nil) ⇒ Integer
Prompts the user for an integer and returns the value.
44 45 46 47 |
# File 'lib/utils/question.rb', line 44 def integer_answer(error_message: nil) error = || File.join('question', 'error', 'integer') string_answer(regex: /^\d+$/, error_message: error).to_i end |
#option_answer(options, error_message: nil) ⇒ Object
Prompts the user to select an option from a given list and returns the selected option.
55 56 57 58 59 60 61 62 63 |
# File 'lib/utils/question.rb', line 55 def option_answer(, error_message: nil) raise 'Options should be an Array' unless .is_a?(Array) raise 'Options should not be empty' if .empty? return .first if .count == 1 .list_all_elements index = read_index_input(count: .count, error_message: ) [index] end |
#string_answer(regex: nil, error_message: nil) ⇒ String
Prompts the user for a string that matches a given regular expression.
70 71 72 73 74 75 |
# File 'lib/utils/question.rb', line 70 def string_answer(regex: nil, error_message: nil) error = || File.join('question', 'error', 'regex') read_input(error_message: error) do |input| regex.nil? || input.match?(regex) end end |