Module: Luhn

Defined in:
lib/luhn.rb,
lib/luhn_ruby/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.valid?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.valid?(number)
  sum = 0
  number.reverse.chars.each_with_index do |ch, index|
    num = ch.to_i
    num *= 2 if index.odd?
    num -= 9 if num >= 10
    sum += num
  end
  (sum % 10).zero?
end