Class: UOB::Payroll::StringCalculator
- Inherits:
-
Object
- Object
- UOB::Payroll::StringCalculator
- Defined in:
- lib/uob/payroll/string_calculator.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Class Method Summary collapse
Instance Method Summary collapse
-
#calculate ⇒ Object
The sum of each byte converted into ASCII multiplied to it’s index.
-
#initialize(string) ⇒ StringCalculator
constructor
A new instance of StringCalculator.
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
#string ⇒ Object (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
#calculate ⇒ Object
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 |