Class: Customizer

Inherits:
Object
  • Object
show all
Defined in:
lib/asker/ai/problem/customizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(problem) ⇒ Customizer

Returns a new instance of Customizer.



4
5
6
# File 'lib/asker/ai/problem/customizer.rb', line 4

def initialize(problem)
  @problem = problem
end

Instance Method Details

#bin2dec(value) ⇒ Object



37
38
39
# File 'lib/asker/ai/problem/customizer.rb', line 37

def bin2dec(value)
  value.to_s.to_i(2)
end

#call(text:, custom:, type: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/asker/ai/problem/customizer.rb', line 8

def call(text:, custom:, type: nil)
  output = text.clone
  custom.each_pair { |oldvalue, newvalue| output.gsub!(oldvalue, newvalue) }

  if type.nil?
    return output
  elsif type == "formula"
    begin
      return eval(output).to_s
    rescue SyntaxError => e
      Logger.error "Problem.name = #{@problem.name}"
      Logger.error "Customizer: Wrong formula '#{text}' or wrong values '#{output}'"
      Logger.error e.to_s
      exit 1
    end
  else
    Logger.error "Customizer: Wrong answer type (#{type})"
    exit 1
  end
end

#dec2bin(value) ⇒ Object



41
42
43
# File 'lib/asker/ai/problem/customizer.rb', line 41

def dec2bin(value)
  value.to_i.to_s(2)
end

#dec2hex(value) ⇒ Object



45
46
47
# File 'lib/asker/ai/problem/customizer.rb', line 45

def dec2hex(value)
  value.to_i.to_s(16)
end

#max(*args) ⇒ Object



33
34
35
# File 'lib/asker/ai/problem/customizer.rb', line 33

def max(*args)
  args.max
end

#min(*args) ⇒ Object



29
30
31
# File 'lib/asker/ai/problem/customizer.rb', line 29

def min(*args)
  args.min
end