Created
December 15, 2015 10:46
-
-
Save RhysStansfield/ce1536e5c8070d82c60f to your computer and use it in GitHub Desktop.
Example quiz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Quiz | |
attr_accessor :questions, :correct, :incorrect | |
def initialize | |
self.questions = [] | |
end | |
def add_question(*args) | |
self.questions << Question.new(*args) | |
end | |
def run | |
questions.each do |question| | |
question.display | |
question.get_answer | |
end | |
self.correct, self.incorrect = questions.partition { |question| question.correct } | |
display_result | |
end_game | |
end | |
def display_result | |
puts "\nYou correctly answered #{correct.size} question(s) out of #{questions.size}" | |
end | |
def end_game | |
puts "\n\nThanks for playing, see you next time!" | |
end | |
end | |
class Question | |
attr_accessor :question, :op1, :op2, :op3, :op4, :answer, :answered_with, :correct | |
def initialize(question, op1, op2, op3, op4, answer) | |
self.question = question | |
self.op1 = op1 | |
self.op2 = op2 | |
self.op3 = op3 | |
self.op4 = op4 | |
self.answer = answer | |
end | |
def get_answer | |
self.answered_with = gets.chomp!.strip.downcase | |
self.correct = answered_with == answer | |
end | |
def display | |
puts '', question, op1, op2, op3, op4, '' | |
puts "Type a letter (a, b, c, d) for your answer" | |
end | |
end | |
quiz = Quiz.new | |
quiz.add_question( | |
"How many places are called Paris in the US?", | |
"a. 17", | |
"b. 6", | |
"c. 25", | |
"d. 12", | |
"a" | |
) | |
quiz.add_question( | |
"What is Uzbekistan's capital city?", | |
"a. Qarshi", | |
"b. Tashkent", | |
"c. Bukhara", | |
"d. Nukus", | |
"b" | |
) | |
quiz.add_question( | |
"What is the most spoken language in Africa?", | |
"a. Swahili", | |
"b. French", | |
"c. Arabic", | |
"d. English", | |
"d" | |
) | |
quiz.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment