class TriviaFactory::MathQuestion

Constants

QUESTION_SUB_TYPES

Public Class Methods

generate() click to toggle source
# File lib/trivia_factory/math_question.rb, line 47
def generate
  sub_type = QUESTION_SUB_TYPES.sample
  question = TriviaFactory::MathQuestion.new(sub_type)
  question
end
new(subtype = :addition) click to toggle source
# File lib/trivia_factory/math_question.rb, line 6
def initialize(subtype = :addition)
  @question_type = :fill_in_the_blank
  @answer_type = :integer
  @choices = []
  raise 'Invalid sub type for TriviaFactory::MathQuestion' unless QUESTION_SUB_TYPES.include?(subtype)
  self.send("build_#{subtype.to_s}")
end

Public Instance Methods

build_addition() click to toggle source
# File lib/trivia_factory/math_question.rb, line 14
def build_addition
  rng = Random.new
  first = rng.rand(100)
  second = rng.rand(100)
  @label = "#{first} + #{second} = _____?"
  @answer = first + second
end
build_division() click to toggle source
# File lib/trivia_factory/math_question.rb, line 38
def build_division
  rng = Random.new
  first = 1 + rng.rand(11)
  second = 1 + rng.rand(11)
  @label = "#{first * second} รท #{first} = _____?"
  @answer = second
end
build_multiplication() click to toggle source
# File lib/trivia_factory/math_question.rb, line 30
def build_multiplication
  rng = Random.new
  first = rng.rand(12)
  second = rng.rand(12)
  @label = "#{first} X #{second} = _____?"
  @answer = first * second
end
build_subtraction() click to toggle source
# File lib/trivia_factory/math_question.rb, line 22
def build_subtraction
  rng = Random.new
  first = rng.rand(100)
  second = rng.rand(100)
  @label = "#{first + second} - #{first} = _____?"
  @answer = second
end