5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/credit_card_validator.rb', line 5
def self.card_number_validate(card_number)
card_number = card_number.to_s.split('')
card_number.each_with_index.map do |value, index|
card_number[index] = value.to_i end
card_digit = card_number[-1]
card_number.pop() card_number = card_number.reverse()
card_number.each_with_index.map do |value, index|
if index.even?
value = value * 2
card_number[index] = value
end
if value > 9
value = value - 9
card_number[index] = value
end
end
total = (card_number.sum()) + card_digit
result = total.modulo(10)
if result == 0
return true
end
return false
end
|