Class: CLI::Quizzer

Inherits:
Object
  • Object
show all
Defined in:
lib/forgetful/cli/quizzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionaire) ⇒ Quizzer

Returns a new instance of Quizzer.



5
6
7
# File 'lib/forgetful/cli/quizzer.rb', line 5

def initialize(questionaire)
  @questionaire = questionaire
end

Instance Attribute Details

#questionaireObject (readonly)

Returns the value of attribute questionaire.



3
4
5
# File 'lib/forgetful/cli/quizzer.rb', line 3

def questionaire
  @questionaire
end

Instance Method Details

#quizObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/forgetful/cli/quizzer.rb', line 9

def quiz
  results = []
  questions = questionaire.questions.sort_by { rand }

  begin
    questions.each_with_index do |question, i|
      q = ask(question, i+1, questions.size)
      results << [question[:id], q]
    end
  rescue EOFError
    # tolerate Ctrl-D, skips the rest of the quiz
  end

  questionaire.grade(results)
end