Class: CreditCardValidations::Luhn
- Inherits:
-
Object
- Object
- CreditCardValidations::Luhn
- Defined in:
- lib/credit_card_validations/luhn.rb
Class Method Summary collapse
Class Method Details
.valid?(number) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/credit_card_validations/luhn.rb', line 14 def self.valid?(number) return false if number.nil? || !number.match?(/\A\d+\z/) s1 = s2 = 0 number.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 |