Ruby Time Conversion

quiz.rb

# Questions pulled from: https://travelswithelle.com/other/multiple-choice-trivia-questions/

require 'json'

# Question object to store the prompt and answer
class Question
  attr_accessor :prompt, :answer
  def initialize(prompt, answer)
    @prompt = prompt
    @answer = answer
  end
end

# This function reads in all the questions from
# the JSON file and returns an array of
# Question objects from it
def select_questions
  data = File.open('./questions.json')
  questions_map = JSON.load data
  data.close
  questions_map.map do |question|
    Question.new(question['prompt'], question['answer'])
  end
end

# Game length selection
def select_length
  puts "Select Quiz Length:\nOption 1: 5 Questions\nOption 2: 10 Questions\nOption 3: 15 Questions\n\nSelect [1,3] to continue."
  option = gets.chomp.to_i
  case option
  when 1
    num_questions = 5
    puts "Selected 5 questions.\n"
  when 2
    num_questions = 10
    puts "Selected 10 questions.\n"
  when 3
    num_questions = 15
    puts "Selected 15 questions.\n"
  else
    puts "Invalid choice. Defaulting to 5 questions."
    num_questions = 5
  end
  num_questions
end

# Main function that gets a list of random
# questions and presents them to the player,
# then tallies and returns the score
def run_quiz
  score = 0
  questions = select_questions.sample(select_length)
  questions.each { |question|
    puts("\n" + question.prompt + "\nInput Answer:")
    answer = gets.chomp.downcase
    if answer == question.answer
      score += 1
    end
  }
  puts "\n---------------------\nYou got #{score} out of #{questions.length}!\n---------------------"
end

run_quiz
    

json

  {
    "prompt": "How many Infinity Stones are there?\na: 3\nb: 5\nc: 6\nd: 10",
    "answer": "c"
  },
  {
    "prompt": "What is the only food that cannot go bad?\na: Dark chocolate\nb: Peanut butter\nc: Canned tuna\nd: Honey",
    "answer": "d"
  },
  {
    "prompt": "Which was René Magritte’s first surrealist painting?\na: Not to Be Reproduced\nb: Personal Values\nc: The Lovers\nd: The Lost Jockey",
    "answer": "d"
  },
  {
    "prompt": "What 90s boy band member bought Myspace in 2011?\na: Nick Lachey\nb: Justin Timberlake\nc: Shawn Stockman\nd: AJ McLean",
    "answer": "b"
  },
  {
    "prompt": "What is the most visited tourist attraction in the world?\na: Eiffel Tower\nb: Statue of Liberty\nc: Great Wall of China\nd: Colosseum",
    "answer": "a"
  },
  {
    "prompt": "What’s the name of Hagrid’s pet spider?\na: Nigini\nb: Crookshanks\nc: Aragog\nd: Mosag",
    "answer": "c"
  },
  {
    "prompt": "What’s the heaviest organ in the human body?\na: Brain\nb: Liver\nc: Skin\nd: Heart",
    "answer": "b"
  },
  {
    "prompt": "Who made the third most 3-pointers in the Playoffs in NBA history?\na: Kevin Durant\nb: JJ Reddick\nc: Lebron James\nd: Kyle Korver",
    "answer": "c"
  },
  {
    "prompt": "Which of these EU countries does not use the euro as its currency?\na: Poland\nb: Denmark\nc: Sweden\nd: All of the above",
    "answer": "d"
  },
  {
    "prompt": "Which US city is the sunniest major city and sees more than 320 sunny days each year?\na: Phoenix\nb: Miami\nc: San Francisco\nd: Austin",
    "answer": "a"
  },
  {
    "prompt": "What type of food holds the world record for being the most stolen around the globe?\na: Wagyu beef\nb: Cheese\nc: Coffee\nd: Chocolate",
    "answer": "b"
  },
  {
    "prompt": "What element does the chemical symbol Au stand for?\na: Silver\nb: Magnesium\nc: Salt\nd: Gold",
    "answer": "d"
  },
  {
    "prompt": "On average, how many seeds are located on the outside of a strawberry?\na: 100\nb: 200\nc: 400\nd: 500",
    "answer": "b"
  },
  {
    "prompt": "In what country do more than half of people believe in elves?\na: Norway\nb: Russia\nc: Holland\nd: Iceland",
    "answer": "d"
  },
  {
    "prompt": "What sport has been played on the moon?\na: Frisbee\nb: Soccer\nc: Golf\nd: Bocce ball",
    "answer": "c"
  }
]
    

Output

Select Quiz Length:
Option 1: 5 Questions
Option 2: 10 Questions
Option 3: 15 Questions

Select [1,3] to continue.
1
Selected 5 questions.

What is the only food that cannot go bad?
a: Dark chocolate
b: Peanut butter
c: Canned tuna
d: Honey
Input Answer:
d

Which of these EU countries does not use the euro as its currency?
a: Poland
b: Denmark
c: Sweden
d: All of the above
Input Answer:
d

What 90s boy band member bought Myspace in 2011?
a: Nick Lachey
b: Justin Timberlake
c: Shawn Stockman
d: AJ McLean
Input Answer:
a

What sport has been played on the moon?
a: Frisbee
b: Soccer
c: Golf
d: Bocce ball
Input Answer:
c

What’s the name of Hagrid’s pet spider?
a: Nigini
b: Crookshanks
c: Aragog
d: Mosag
Input Answer:
a

---------------------
You got 3 out of 5!
---------------------