Module: Input

Included in:
ChessEngine::CLI
Defined in:
lib/chess_engine/input.rb

Defined Under Namespace

Classes: Incorrect

Instance Method Summary collapse

Instance Method Details

#get_input(input_message, regex = nil, err_message = "Incorrect input, try again") ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/chess_engine/input.rb', line 4

def get_input(input_message, regex = nil, err_message = "Incorrect input, try again")
  begin
    print input_message
    input = gets.chomp
    if block_given?
      raise Input::Incorrect unless yield(input)
    else
      raise Input::Incorrect unless regex.match?(input)
    end
  rescue
    puts err_message
    retry
  end
  input
end