Module: Luhn
- Defined in:
- lib/luhn.rb,
lib/luhn/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Class Method Summary collapse
- .calculate_check_digit(number) ⇒ Object
- .double_digits(number) ⇒ Object
- .get_check_digit(number) ⇒ Object
- .ping ⇒ Object
- .valid?(number) ⇒ Boolean
Class Method Details
.calculate_check_digit(number) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/luhn.rb', line 15 def calculate_check_digit(number) number_without_check_digit = number.to_s[0...-1] + '0' doubles = double_digits(number_without_check_digit.to_i) sum = sum_of_all(doubles) unless null?(doubles) product = sum * 9 get_check_digit(product) end |
.double_digits(number) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/luhn.rb', line 27 def double_digits(number) doubles = [] digits = all_digits(number).reverse digits.each_with_index do |d, i| if i.odd? sum = d * 2 if sum && sum > 9 split_sum = sum.to_s.split(//).map(&:to_i) make_sum = split_sum.inject { |sum, n| sum + n } doubles << make_sum else doubles << sum end else doubles << d end end return doubles end |
.get_check_digit(number) ⇒ Object
23 24 25 |
# File 'lib/luhn.rb', line 23 def get_check_digit(number) number.to_s[-1].to_i end |
.ping ⇒ Object
7 8 9 |
# File 'lib/luhn.rb', line 7 def ping 'pong' end |
.valid?(number) ⇒ Boolean
11 12 13 |
# File 'lib/luhn.rb', line 11 def valid?(number) checksum(number) end |