Module: Credy::Check

Extended by:
Check
Included in:
Check
Defined in:
lib/credy/check.rb

Instance Method Summary collapse

Instance Method Details

#luhn(code) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/credy/check.rb', line 5

def luhn(code)
  s1 = s2 = 0

  code.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