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

Version Path
banktools-se-3.2.0 lib/banktools-se/utils.rb
banktools-se-3.1.1 lib/banktools-se/utils.rb
banktools-se-3.1.0 lib/banktools-se/utils.rb
banktools-se-3.0.1 lib/banktools-se/utils.rb
banktools-se-3.0.0 lib/banktools-se/utils.rb
banktools-se-2.6.3 lib/banktools-se/utils.rb
banktools-se-2.6.2 lib/banktools-se/utils.rb
banktools-se-2.6.1 lib/banktools-se/utils.rb
banktools-se-2.6.0 lib/banktools-se/utils.rb
banktools-se-2.5.0 lib/banktools-se/utils.rb
banktools-se-2.4.0 lib/banktools-se/utils.rb
banktools-se-2.3.0 lib/banktools-se/utils.rb
banktools-se-2.1.0 lib/banktools-se/utils.rb
banktools-se-2.0.0 lib/banktools-se/utils.rb
banktools-se-1.0.0 lib/banktools-se/utils.rb
banktools-se-0.11.0 lib/banktools-se/utils.rb
banktools-se-0.10.0 lib/banktools-se/utils.rb
banktools-se-0.8.0 lib/banktools-se/utils.rb
banktools-se-0.7.0 lib/banktools-se/utils.rb
banktools-se-0.6.0 lib/banktools-se/utils.rb