From: James Edward Gray II Date: 2007-05-01T20:55:14+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--2668181 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: Francois Lafortune > Date: May 1, 2007 3:01:56 AM CDT > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Hello, this is my first attempt at RubyQuiz... I haven't gotten > around to understanding the whole ruby-talk mailing list concept so > I thought I'd just send it to you. > This is for quiz #122 (The credit card validation thingy)... > > BTW, loved your book and love your site. I learn a LOT from them! > --Apple-Mail-1--2668181 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name=card_checker.rb Content-Disposition: attachment; filename=card_checker.rb #!/usr/bin/env ruby # rubyquiz #122 # I know, I know, this code is ugly, but I had fun and this is my first Ruby Quiz! :P def card_type(num) size = num.size.to_s { 'AMEX' => [%r(^34|37),%r(15)], 'Discover' => [%r(^(6011/16)),%r(16)], 'MasterCard' => [%r(^(51|55)),%r(16)], 'Visa' => [%r(^4),%r(13|16)] }.each_pair do |n,c| return n unless !(num.match(c[0]) && size.match(c[1])) end return 'unknown' end def luhn(num) d = num.split(//) ((0..d.size).inject(0){|a,i| a += (d[i].to_i * (2-i%2)).to_s.split(//).inject(0){|r,n| r + n.to_i}} % 10).zero? end num = ARGV.join.delete(",") puts "\nCard: #{card_type(num)}\nValid: #{luhn(num)}" --Apple-Mail-1--2668181 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed > > ________________________ > Francois Lafortune > quickredfox@neuenode.com > --Apple-Mail-1--2668181--