-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluhn.rb
63 lines (48 loc) · 1.16 KB
/
luhn.rb
1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Luhn
def initialize
cc_num = ARGV[0]
if cc_num.is_a? String
pre_luhn cc_num
else
puts "Please provide String only"
end
end
def pre_luhn cc_num
cc_num = cc_num.gsub("-","") if cc_num.include? "-"
@temp_s = cc_num.split ""
unless @temp_s.size == 16
puts "Please provide 16 digits number"
else
execute_check_luhn cc_num
end
end
def execute_check_luhn cc_num
unless @temp_s.include? "?"
validate_luhn cc_num
else
i = 0
until i > 9 do
temp = cc_num.gsub "?", i.to_s
validate_luhn temp
i +=1
end
end
end
def validate_luhn(cardNumber)
nums = cardNumber.to_s.split("")
checkdigit = nums[nums.length - 1]
nums[nums.length - 1] = 0
nums.reverse!
sum=0
for i in 1..nums.length
if i%2==0
sum = sum + nums[i].to_i
next
end
nums[i] = (nums[i].to_i*2 < 10 ) ? (nums[i].to_i*2) : (nums[i].to_i*2 - 9)
sum = sum + nums[i].to_i
end
puts (10 - sum%10).to_i == checkdigit.to_i ? "#{cardNumber} (valid luhn)" : "#{cardNumber} (invalid luhn)"
end
end
Luhn.new