This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Checking a Credit Card Checksum
CodeExpectedActual
require 'rubygems'
require 'creditcard'
'5276 4400 6542 1319'.creditcard?
true true
'5276440065421313'.creditcard?
false false
1276440065421319.creditcard?
false false
5276440065421313.creditcard_type
"mastercard" "mastercard"
module CreditCard
  def creditcard?
    numbers = self.to_s.gsub(/[^\d]+/, '').split(//)
    checksum = 0
    0.upto numbers.length do |i|
      weight = numbers[-1*(i+2)].to_i * (2 - (i%2))
      checksum += weight % 9
    end
    return numbers[-1].to_i == 10 - checksum % 10
  end
end
class String
  include CreditCard
end
class Integer
  include CreditCard
end
'5276 4400 6542 1319'.creditcard?
true true
numbers = '5276 4400 6542 1319'.gsub(/[^\d]+/, '').split(//)
["5", "2", "7", "6", "4", "4", "0", "0", ["5", "2", "7", "6", "4", "4", "0", "0", "6", "5", "4", "2", "1", "3", "1", "9"]
checksum = 0
0.upto numbers.length do |i|
  weight = numbers[-1*(i+2)].to_i * (2 - (i%2))
  checksum += weight % 9
end
checksum
51 51
numbers[-1].to_i == 10 - checksum % 10
true true