class UOB::Payroll::StringCalculator

Attributes

string[R]

Public Class Methods

calculate(string) click to toggle source
# File lib/uob/payroll/string_calculator.rb, line 8
def calculate(string)
  new(string).calculate
end
new(string) click to toggle source
# File lib/uob/payroll/string_calculator.rb, line 13
def initialize(string)
  @string = string
end

Public Instance Methods

calculate() click to toggle source

The sum of each byte converted into ASCII multiplied to it's index. For example: BLA #=> 413 B ~> 66 * 1 L ~> 76 * 2 A ~> 65 * 3

# File lib/uob/payroll/string_calculator.rb, line 23
def calculate
  string.
    each_byte.
    to_a.
    each_with_index.
    map { |byte, index| (index + 1) * byte }.
    reduce(0, :+)
end