DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
A Quiz Engine In Ruby
// A Quiz Engine in Ruby
# A Quiz Engine in Ruby
# By Jake Burton <jake5991@live.com>
# The author disclaims copyright to this source code. And Also wishes you all the best.
# Variables to customize
quizname = "QUIZNAME (STRING)"
score = 0
# Setting Up
class Questions
attr_accessor :question, :answer
def initialize(params)
@question = params[:question]
@answer = params[:answer]
end
end
# A question number Array so we can get random questions from the hash. - There is probably a better way of doing this.
nums = (1..10).to_a
# Create Questions and Answers
qs = {
1 => Questions.new( :question => "QUESTION1", :answer => "ANSWER1" ),
2 => Questions.new( :question => "QUESTION2", :answer => "ANSWER2" ),
3 => Questions.new( :question => "QUESTION3", :answer => "ANSWER3" ),
4 => Questions.new( :question => "QUESTION4", :answer => "ANSWER4" ),
5 => Questions.new( :question => "QUESTION5", :answer => "ANSWER5" ),
6 => Questions.new( :question => "QUESTION6", :answer => "ANSWER6" ),
7 => Questions.new( :question => "QUESTION7", :answer => "ANSWER7" ),
8 => Questions.new( :question => "QUESTION8", :answer => "ANSWER8" ),
9 => Questions.new( :question => "QUESTION9", :answer => "ANSWER9" ),
10 => Questions.new( :question => "QUESTION10", :answer => "ANSWER10" )
}
# Engine Logic
playstate = true
while playstate == true do
puts "Quiz: " + quizname
puts "Commands; new = new question"
puts "Current Score:"
puts score
instruction = gets
next_question = case instruction.chomp
when "new"
torf = false
n = nums[rand(nums.length)]
puts "Question:"
puts qs[n].question
torf = gets.downcase.chomp.eql? qs[n].answer.downcase.to_str
if torf == true
puts ""
puts "CORRECT"
puts ""
score += 1
else
puts ""
puts "INCORRECT"
puts ""
end
when "exit"
puts "Thankyou for Playing"
exit
# Change this to set playstate to false to leave the loop and continue.
end
end





