Create a simple Quiz using Ruby!
Jaspreet kaur
Posted on November 4, 2022
I am new to coding, and it's been a week I have started learning Ruby. For practicing the concepts I have learnt so far, I did a quiz exercise. As a beginner, I feel until & unless you start coding actually, you might always be in doubt if you have understood the concepts thoroughly or if you can implement your leanings confidently. Doing these small exercises can make your concepts clearer.
So lets begin constructing a simple multiple choice quiz in ruby -
Step 1 >>
- Create a class Quiz.
- Inside this class - Initialize question and answer attributes
class Quiz
attr_accessor :question, :answer
def initialize(question, answer)
@question=question
@answer= answer
end
end
Step 2 >>
After you create the class, you can write the questions you want to ask along with the answer choices. Each question will be assigned to a variable.
question1= "How many sides does a square have?\n(a) 2\n(b) 4\n(c) 6"
question2 = "When it's raining what do you need?\n(a) A parachute\n(b) A teapot\n(c) An umbrella"
question3= "What sea creature has 8 legs?\n(a) An octopus\n(b) A dolphin\n(c) A crocodile"
Step 3 >>
- Create an array of questions.
- Inside this array we will create new question instances or objects using our Quiz class (Quiz.new).
- Since we have initialized question and answer attributes above, we can pass these attributes to our new instances.
- Each question is a new instance/object which will have unique question (questions that we created ) and answer( the correct choice) attribute.
Now we have an array that stores all the questions for our quiz with correct answers.
questions= [
Quiz.new(question1,"b"),
Quiz.new(question2,"c"),
Quiz.new(question3,"a")
]
Step 4 >>
- Next we need to run the Quiz.
- For this we need to create a method that will run our quiz and calculate the score.
- This method should be able to go through each question in the array and ask the user that particular question, get their answer and check if that answer is correct or not.
def run_quiz(questions)
answer=""
for item in questions
puts item.question
answer= gets.chomp()
end
end
- In the code above, we are passing questions array to the run_quiz method.
- We created an answer variable with (""), This basically will store the user's answers.
- Next we create a loop to iterate through each item in the questions array, and prints the question for user. (item.question), we called the question attribute on item which will display the question(question1/ question2/ question3)for the current item we are looping through.
- Then we are getting user's answer (answer= gets.chomp()) (.chomp() basically removes the new line that gets entered when users click enter).
Step 5 >>
- Find the Score.
- Add a new score variable with 0 initial value.
- With the help of if statement find out if your answer is correct or not. If correct add 1 to the score each time.
def run_quiz(questions)
answer=""
score=0
for item in questions
puts item.question
answer= gets.chomp()
if answer==item.answer
score+=1
end
end
puts ("Your Score is " + score.to_s + "/"+ questions.length().to_s)
end
Step 6 >>
Finally , call the run_quiz method with argument of questions.
Overall the code looks like below -
class Quiz
attr_accessor :question, :answer
def initialize(question, answer)
@question=question
@answer= answer
end
end
question1= "How many sides does a square have?\n(a) 2\n(b) 4\n(c) 6"
question2 = "When it's raining what do you need?\n(a) A parachute\n(b) A teapot\n(c) An umbrella"
question3= "What sea creature has 8 legs?\n(a) An octopus\n(b) A dolphin\n(c) A crocodile"
questions= [
Quiz.new(question1,"b"),
Quiz.new(question2,"c"),
Quiz.new(question3,"a")
]
def run_quiz(questions)
answer=""
score=0
for item in questions
puts item.question
answer= gets.chomp()
if answer==item.answer
score+=1
end
end
puts ("Your Score is " + score.to_s + "/"+ questions.length().to_s)
end
run_quiz(questions)
Step 7>>
Run your ruby file in the terminal, quiz should look like below-
And that's it. Hope you enjoyed constructing your own quiz!!
Posted on November 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.