Class: GatesOfMoria::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/gates_of_moria/repl.rb

Instance Method Summary collapse

Constructor Details

#initialize(output_buffer, input_buffer, questions) ⇒ REPL

Returns a new instance of REPL.



3
4
5
6
7
8
9
# File 'lib/gates_of_moria/repl.rb', line 3

def initialize(output_buffer, input_buffer, questions)
  @output_buffer = output_buffer
  @input_buffer = input_buffer

  @questions = JSON.parse(questions)['questions']
  @last_question = @questions.pop
end

Instance Method Details

#answer_question(expected_answer, user_input) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gates_of_moria/repl.rb', line 16

def answer_question(expected_answer, user_input)
  begin
    result = eval(user_input)
  rescue SystemExit
    display_leaving_game
    exit 0
  rescue SyntaxError, Exception
    politely_say_their_ruby_is_invalid
    return false
  end
  @output_buffer.puts "\n=> #{result}\n\n"
  eval(expected_answer)
end

#startObject



11
12
13
14
# File 'lib/gates_of_moria/repl.rb', line 11

def start
  ask_the_questions
  ask_last_question
end