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).to_s
end

#bin2hex(value) ⇒ Object



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

def bin2hex(value)
  dec = bin2dec(value)
  dec2hex(dec)
end

#bin2oct(value) ⇒ Object



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

def bin2oct(value)
  dec = bin2dec(value)
  dec2oct(dec)
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



51
52
53
# File 'lib/asker/ai/problem/customizer.rb', line 51

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

#dec2hex(value) ⇒ Object



55
56
57
# File 'lib/asker/ai/problem/customizer.rb', line 55

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

#dec2oct(value) ⇒ Object



59
60
61
# File 'lib/asker/ai/problem/customizer.rb', line 59

def dec2oct(value)
  value.to_i.to_s(8).to_s
end

#hex2bin(value) ⇒ Object



63
64
65
66
# File 'lib/asker/ai/problem/customizer.rb', line 63

def hex2bin(value)
  dec = hex2dec(value)
  dec2bin(dec)
end

#hex2dec(value) ⇒ Object



68
69
70
# File 'lib/asker/ai/problem/customizer.rb', line 68

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

#hex2oct(value) ⇒ Object



72
73
74
75
# File 'lib/asker/ai/problem/customizer.rb', line 72

def hex2oct(value)
  dec = hex2dec(value)
  dec2oct(dec)
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

#oct2bin(value) ⇒ Object



77
78
79
80
# File 'lib/asker/ai/problem/customizer.rb', line 77

def oct2bin(value)
  dec = oct2dec(value)
  dec2bin(dec)
end

#oct2dec(value) ⇒ Object



82
83
84
# File 'lib/asker/ai/problem/customizer.rb', line 82

def oct2dec(value)
  value.to_s.to_i(8).to_s
end

#oct2hex(value) ⇒ Object



86
87
88
89
# File 'lib/asker/ai/problem/customizer.rb', line 86

def oct2hex(value)
  dec = oct2dec(value)
  dec2hex(dec)
end