Class: UOB::Payroll::StringCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/uob/payroll/string_calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringCalculator

Returns a new instance of StringCalculator.



13
14
15
# File 'lib/uob/payroll/string_calculator.rb', line 13

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



5
6
7
# File 'lib/uob/payroll/string_calculator.rb', line 5

def string
  @string
end

Class Method Details

.calculate(string) ⇒ Object



8
9
10
# File 'lib/uob/payroll/string_calculator.rb', line 8

def calculate(string)
  new(string).calculate
end

Instance Method Details

#calculateObject

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



23
24
25
26
27
28
29
30
# 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