# Re: Checking Credit Cards (#122)

**URL:** <https://www.ruby-forum.com/t/re-checking-credit-cards-122/103194>\
**Category:** Ruby\
**Created:** [May 2, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/re-checking-credit-cards-122/103194 "2007-05-02T20:38:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaserx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ec9cab/32.png) [@chaserx](https://www.ruby-forum.com/u/chaserx)\
**Post date:** [May 2, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/re-checking-credit-cards-122/103194/1 "2007-05-02T20:38:04Z")

</div>

This is my first time submitting a solution to the quiz. I’ll attach my  
.rb  
file and copy / paste my solution below. It’s ugly, but it works.

Thanks,

Chase Southard

#!/usr/bin/env ruby

# 

# Created by Chase Southard on 2007-05-01.

class CardValidator

def get\_card\_number  
if $\*[0] == nil  
puts “I need a card number. Please enter one now:”  
cardnumber = $stdin.gets.strip!  
else  
cardnumber = ARGV.shift  
end  
puts “You entered: #{cardnumber}”  
@card\_array = cardnumber.split(//).collect  
#remove spaces  
@card\_array.compact!  
end

def validate\_card\_prefix  
#case statement to grab the first few digits  
case @card\_array.first.to\_i  
when 3  
prefix = @card\_array.slice(0, 2).join.to\_i  
#debug  
#puts prefix  
#puts prefix.class  
if prefix == 34 || 37  
if @card\_array.length == 15  
card\_type = “AMEX”  
puts “The card type entered was: #{card\_type}”  
else  
“You might have bogus card.”  
end  
else  
“You might have a bogus card.”  
end  
when 6  
prefix = @card\_array.slice(0,4).join.to\_i  
#debug  
#puts prefix  
#puts prefix.class  
#puts “card length: #{@card\_array.length}”  
if prefix == 6011 && @card\_array.length == 16  
card\_type = “Discover”  
puts “The card type entered was: #{card\_type}”  
else  
“You might have a bogus card.”  
end  
when 4  
if @card\_array.length == 13 || 16  
card\_type = “Visa”  
puts “The card type entered was: #{card\_type}”  
else  
“You might have a bogus card.”  
end  
when 5  
prefix = @card\_array.slice(0, 2).join.to\_i  
#puts prefix  
#puts prefix.class  
if prefix == 51 || 52 || 53 || 54  
if @card\_array.length == 16  
card\_type = “MasterCard”  
puts “The card type entered was: #{card\_type}”  
else  
“You might have a bogus card.”  
end  
end  
end  
end

def validate\_luhn  
array\_size = @card\_array.length

```
#starting at the LAST digit and working backwards gathering digits 

```

to  
get the untouched elements of the card number

```
untouched_elements_index = Array.new
while array_size > 1
  untouched_elements_index.push(array_size - 1)
  array_size -= 2
end

untouched_elements = Array.new
untouched_elements_index.each { |e|

```

untouched\_elements.push(@card\_array[e].to\_i) }

```
#reset array size for the next part
array_size = @card_array.length

#Starting at the NEXT TO LAST digit and working backwards gathering

```

digits to get the touched elements of the card number  
every\_other\_element = Array.new  
while array\_size \> 1  
every\_other\_element.push(array\_size - 2)  
array\_size -= 2  
end

```
#debug
#puts "Every other element: #{every_other_element}"

#multiply each element by 2
touched_array = Array.new
every_other_element.each { |element|

```

touched\_array.push(@card\_array[element].to\_i\*2) }

```
#debug
#puts "Touched array: #{touched_array}"

#split each into digits
split_touched_array = Array.new
touched_array.each { |e| split_touched_array.push(e.to_s.split(//)) 

```

}  
split\_touched\_array.flatten!

```
#debug
#puts "Split touched array:"
#puts split_touched_array

#return digits to integer form
split_touched_array_integers = Array.new
split_touched_array.each { |e| 

```

split\_touched\_array\_integers.push(e.to\_i)  
}

```
#debug
#split_touched_array_total = 0
#split_touched_array_integers.each { |e| split_touched_array_total 

```

+= e  
}  
#puts “split total = #{split\_touched\_array\_total}”

```
#concatentate the two arrays
all_digit_array = untouched_elements + split_touched_array_integers

#find the total value of the touched and untouched digits from the 

```

card  
number  
array\_total\_value = 0  
all\_digit\_array.each { |x| array\_total\_value += x }

```
#debug
#puts "array total value = #{array_total_value}"

#final determination of validity by Luhn algorithm
@numerical_validation = false

if array_total_value % 10 == 0
  @numerical_validation = true
  puts "This card number, #{@card_array}, is valid [by Luhn 

```

algorithm]"  
else  
puts “This card number, #{@card\_array}, is in-valid [by Luhn  
algorithm]”  
end

end

end

my\_card = CardValidator.new  
my\_card.get\_card\_number  
my\_card.validate\_card\_prefix  
my\_card.validate\_luhn
