Class: CreditCardValidations::Luhn

Inherits:
Object
  • Object
show all
Defined in:
lib/credit_card_validations/luhn.rb

Class Method Summary collapse

Class Method Details

.valid?(number) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/credit_card_validations/luhn.rb', line 8

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