Ruby - Programming Language
This course covers the basics of programming in Ruby. Work your way through the videos and we'll teach you everything you need to know to start your programming journey!

Creating A Quiz

Lesson 32
Author : Mike Dane
Last Updated : November, 2017


Code

Copyclass Question
     attr_accessor :prompt, :answer
     def initialize(prompt, answer)
          @prompt = prompt
          @answer = answer
     end
end

p1 = "What color are apples?\n(a) Red/Green\n(b)Orange"
p2 = "What color are bananas?\n(a) Red/Green\n(b)Yellow"

questions = [
     Question.new(p1, "a"),
     Question.new(p2, "b")
]

def run_quiz(questions)
     answer = ""
     score = 0
     for question in questions
          puts question.prompt
          answer = gets.chomp()
          if answer == question.answer
               score += 1
          end
     end
     puts "you got #{score} out of #{questions.length()}"
end

run_quiz(questions)