Sha256: a587164ef31171260d854b9de3306a3f3c7c5d995a00394e3ab358f483c01fd1
Contents?: true
Size: 797 Bytes
Versions: 21
Compression:
Stored size: 797 Bytes
Contents
module BankTools module SE module Utils # Based on http://blog.internautdesign.com/2007/4/18/ruby-luhn-check-aka-mod-10-formula def self.valid_luhn?(number) digits = number.to_s.scan(/\d/).reverse.map { |x| x.to_i } digits = digits.each_with_index.map { |d, i| d *= 2 if i.odd? d > 9 ? d - 9 : d } sum = digits.inject(0) { |m, x| m + x } sum % 10 == 0 end def self.luhn_checksum(number) digits = number.to_s.scan(/\d/).reverse.map { |x| x.to_i } digits = digits.each_with_index.map { |d, i| d *= 2 if i.even? d > 9 ? d - 9 : d } sum = digits.inject(0) { |m, x| m + x } mod = 10 - sum % 10 mod == 10 ? 0 : mod end end end end
Version data entries
21 entries across 21 versions & 1 rubygems