Sha256: bc72775576eb24b912e67042801009916cda858069bf035a597164a59b014b01

Contents?: true

Size: 350 Bytes

Versions: 2

Compression:

Stored size: 350 Bytes

Contents

module CreditCardDetector
  # Class to validate Luhn numbers.
  class Luhn
    def self.valid?(number)
      s1 = s2 = 0

      number.to_s.reverse.chars.each_slice(2) do |odd, even|
        s1 += odd.to_i

        double = even.to_i * 2
        double -= 9 if double >= 10
        s2 += double
      end

      (s1 + s2) % 10 == 0
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
credit_card_detector-0.4 lib/credit_card_detector/luhn.rb
credit_card_detector-0.3 lib/credit_card_detector/luhn.rb