Class: Aritcaptcha::Calculation

Inherits:
Object
  • Object
show all
Defined in:
lib/aritcaptcha/calculation.rb

Class Method Summary collapse

Class Method Details

.generate_calculation(number1, number2, operator, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aritcaptcha/calculation.rb', line 5

def self.generate_calculation number1, number2, operator, &block
  # Division by zero
  return [nil, nil] if operator == "/" and number1.zero? or number2.zero?

  number1 = rand(number1)
  number2 = rand(number2)

  equation = "#{number1} #{operator} #{number2}"
  result = eval("#{number1.to_f} #{operator} #{number2.to_f}")

  if result.infinite?
    # over and over again untill !infnite
    number1 = rand(number1)
    number2 = rand(number2)
    result = eval("#{number1.to_f} #{operator} #{number2.to_f}")
  end

  result = (result == result.floor) ? result.to_i : result
  [equation, result]
end