Module: AN::Luhn

Defined in:
lib/an.rb

Overview

Constant Summary collapse

RELATIVE_NUM =
{ '0' => 0, '1' => 2, '2' => 4, '3' => 6, '4' => 8,
'5' => 1, '6' => 3, '7' => 5, '8' => 7, '9' => 9 }

Class Method Summary collapse

Class Method Details

.check(number) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/an.rb', line 175

def self.check(number)
  number = number.to_s.gsub(/\D/, "").reverse

  sum = 0

  number.split("").each_with_index do |n, i|
    sum += (i % 2 == 0) ? n.to_i : RELATIVE_NUM[n]
  end

  sum % 10 == 0
end