Sha256: e0ca6fe5a2082d53e24c93164f6954347de80d75ce19fd1842d5448fbd15f986

Contents?: true

Size: 635 Bytes

Versions: 11

Compression:

Stored size: 635 Bytes

Contents

module UOB
  module Payroll
    class StringCalculator

      attr_reader :string

      class << self
        def calculate(string)
          new(string).calculate
        end
      end

      def initialize(string)
        @string = string
      end

      # 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
      def calculate
        string.
          each_byte.
          to_a.
          each_with_index.
          map { |byte, index| (index + 1) * byte }.
          reduce(0, :+)
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
uob-payroll-1.2.0 lib/uob/payroll/string_calculator.rb
uob-payroll-1.1.0 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.8 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.7 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.6 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.5 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.4 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.3 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.2 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.1 lib/uob/payroll/string_calculator.rb
uob-payroll-1.0.0 lib/uob/payroll/string_calculator.rb