Module: Israeli::Luhn
- Defined in:
- lib/israeli/luhn.rb
Overview
Luhn algorithm (mod 10) implementation for checksum validation.
Used by Israeli ID numbers (Mispar Zehut) and potentially business registration numbers. The Israeli variant uses a left-to-right processing order (index 0 = multiplier 1, index 1 = multiplier 2, etc.).
Class Method Summary collapse
-
.valid?(digits) ⇒ Boolean
Validates a string of digits using the Luhn algorithm.
Class Method Details
.valid?(digits) ⇒ Boolean
Validates a string of digits using the Luhn algorithm.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/israeli/luhn.rb', line 23 def self.valid?(digits) return false if digits.nil? || digits.empty? return false unless digits.match?(/\A\d+\z/) sum = digits.chars.each_with_index.sum do |char, index| digit = char.to_i * (index.even? ? 1 : 2) digit > 9 ? digit - 9 : digit end (sum % 10).zero? end |