I can't write Ruby code, but I found this ruby code to calculate checksum for AEMO NMI
def checksum
summation = 0
@nmi.reverse.split(//).each_index do |i|
value = nmi[nmi.length - i - 1].ord
value *= 2 if i.even?
value = value.to_s.split(//).map(&:to_i).reduce(:+)
summation += value
end
checksum = (10 - (summation % 10)) % 10
checksum
end
Could someone please help me to explain what does this line mean?
value = value.to_s.split(//).map(&:to_i).reduce(:+)
I try to convert the code above to VBA for excel.
For an input of "4103738516" will give you 8 "4102030716" ==> 2 "QFFF0000LV" ==> 7
in page 40 of this document has JavaScript code to calculate it but I can't understand the code.
https://www.aemo.com.au/-/media/Files/PDF/0610-0008-pdf.pdf
Thank you
Below code should make you understand that statement better:
# Say
value = 82478923 # any random number
value.to_s # => "82478923"
# `split(//)` is similar to `split` and `split('')`, they split a string after each character to generate an array of characters.
ary = value.to_s.split(//) # => ["8", "2", "4", "7", "8", "9", "2", "3"]
ary.map(&:to_i) # => [8, 2, 4, 7, 8, 9, 2, 3]
# `inject(:+)` will iterate the array and calculate sum of all numbers
ary.map(&:to_i).inject(:+) # => 43
Read more about inject here.