Class: LuhneyBin

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

Class Method Summary collapse

Class Method Details

.validate(str) ⇒ Object

inspired by concise python code on en.wikipedia.org/wiki/Luhn



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/luhney_bin.rb', line 3

def self.validate(str)
  total = 0
  double_next = false
  str.reverse.each_char do |d|
    if double_next
      total += 2 * d.to_i % 9
    else
      total += d.to_i
    end
    double_next = !double_next
  end
  
  total % 10 == 0
end