Class: Keisanjaku::QuestionGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/keisanjaku/drill.rb

Constant Summary collapse

OPERATIONS =
%i[multiply divide square cube sqrt log sin tan].freeze

Instance Method Summary collapse

Constructor Details

#initialize(random: Random.new) ⇒ QuestionGenerator

Returns a new instance of QuestionGenerator.



12
13
14
# File 'lib/keisanjaku/drill.rb', line 12

def initialize(random: Random.new)
  @random = random
end

Instance Method Details

#next_question(operation: OPERATIONS.sample(random: @random)) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/keisanjaku/drill.rb', line 16

def next_question(operation: OPERATIONS.sample(random: @random))
  operation = operation.to_sym
  raise ArgumentError, "unknown operation: #{operation}" unless OPERATIONS.include?(operation)

  case operation
  when :multiply
    a = sample_mantissa
    b = sample_mantissa
    build("#{a}×#{b}", "#{a} × #{b} を求めよ", operation)
  when :divide
    a = sample_mantissa
    b = sample_mantissa
    build("#{a}÷#{b}", "#{a} ÷ #{b} を求めよ", operation)
  when :square
    a = sample_mantissa(max: 9.0)
    build("#{a}^2", "#{a} の二乗を求めよ", operation)
  when :cube
    a = sample_mantissa(max: 9.0)
    build("#{a}^3", "#{a} の三乗を求めよ", operation)
  when :sqrt
    a = sample_mantissa(max: 9.0)
    build("sqrt(#{a})", "#{a} の平方根を求めよ", operation)
  when :log
    a = sample_mantissa
    build("log(#{a})", "#{a} の常用対数を求めよ", operation)
  when :sin
    angle = @random.rand(6..89)
    build("sin(#{angle})", "sin(#{angle}) を求めよ", operation)
  when :tan
    angle = @random.rand(6..45)
    build("tan(#{angle})", "tan(#{angle}) を求めよ", operation)
  else
    raise ArgumentError, "unknown operation: #{operation}"
  end
end