class NerdQuiz::Quiz
Public Class Methods
new(input, output, scorecard)
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 5 def initialize(input, output, scorecard) @input = input @output = output @scorecard = scorecard end
Public Instance Methods
run()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 11 def run handle_signals questions start while @scorecard.incomplete? ask listen end over ensure bye end
Private Instance Methods
answer()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 77 def answer @question.answer end
ask()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 30 def ask out("Question #{@scorecard.next_question + 1} #{label}:", :blue) out(question) out('>> ', :blue, :bold, :no_new_line) end
bye()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 26 def bye out('Bye!', :blue, :bold) end
handle_signals()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 92 def handle_signals Signal.trap('INT', 'EXIT') end
label()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 68 def label @question = @questions.pop @question.label end
listen()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 36 def listen if reply == answer out('Right!', :green, :bold) @scorecard.right_answer! else out('Wrong!', :red, :bold) # Do not nest ANSI sequences, so directly call set_color out(set_color('You should have answered ', :red) + set_color(answer, :red, true)) @scorecard.wrong_answer! end end
out(*args)
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 81 def out(*args) text = args[0] unless args[1].nil? color = args[1] || false bold = args[2].is_a?(Symbol) ? true : false text = set_color(text, color, bold) end method = args[3].nil? ? :puts : :print @output.send(method, text) end
over()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 59 def over out('Thanks For Playing!', :blue) out("Final Score #{@scorecard.score}", :yellow) end
question()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 73 def question @question.text end
questions()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 64 def questions @questions = NerdQuiz::Questions.get end
reply()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 48 def reply reply = @input.gets # Ctrl-D (EOF) user action cause @input.gets to be nil # On this action, lets exit like the user asked reply.nil? ? exit : reply.strip end
start()
click to toggle source
# File lib/nerd_quiz/quiz.rb, line 55 def start out('Welcome to Nerd Quiz', :blue) end