# Module: Sjekksum::Luhn

Extended by:
Luhn, Shared
Included in:
Luhn
Defined in:
lib/sjekksum/luhn.rb

## Overview

Module for calculation and validation of Luhn checksums

## Instance Method Details

### #convert(number) ⇒ Integer, StringAlso known as: transform

Transforms a number by appending the Luhn checksum digit

Examples:

Sjekksum::Luhn.convert(7992739871) #=> 79927398713

Parameters:

• number (Integer, String)

number without a checksum

Returns:

• (Integer, String)

final number including the checksum

 57 58 59 60 # File 'lib/sjekksum/luhn.rb', line 57 def convert number raise_on_type_mismatch number typed_conversion number end

### #of(number) ⇒ IntegerAlso known as: checksum

Calculates Luhn checksum

Examples:

Sjekksum::Luhn.of(7992739871) #=> 3

Parameters:

• number (Integer, String)

number for which the checksum should be calculated

Returns:

• (Integer)

calculated checksum

 20 21 22 23 24 25 26 27 28 29 # File 'lib/sjekksum/luhn.rb', line 20 def of number raise_on_type_mismatch number digits = convert_number_to_digits(number) sum = digits.reverse.map.with_index do |digit, idx| idx.even? ? (digit * 2).divmod(10).reduce(&:+) : digit end.reduce(&:+) (10 - sum % 10) % 10 end

### #valid?(number) ⇒ BooleanAlso known as: is_valid?

Luhn validation of provided number

Examples:

Sjekksum::Luhn.valid?(79927398713) #=> true

Parameters:

• number (Integer, String)

number with included checksum

Returns:

• (Boolean)
 41 42 43 44 45 # File 'lib/sjekksum/luhn.rb', line 41 def valid? number raise_on_type_mismatch number num, check = split_number(number) self.of(num) == check end