class CLI::Quizzer
Attributes
questionaire[R]
Public Class Methods
new(questionaire)
click to toggle source
# File lib/forgetful/cli/quizzer.rb, line 5 def initialize(questionaire) @questionaire = questionaire end
Public Instance Methods
quiz()
click to toggle source
# 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
Private Instance Methods
ask(question, i, n)
click to toggle source
# File lib/forgetful/cli/quizzer.rb, line 26 def ask(question, i, n) width = "#{n}/#{n}. ".size padding = " " * width print "#{i}/#{n}. ".rjust(width) + "Q: #{question[:question]}" readline puts padding + "A: #{question[:answer]}" while true print padding + "? " answer = readline.chomp if answer =~ /\A[0-5]\Z/ puts return answer.to_i end end end