------art_356970_20361627.1178026680671
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

The only reason I'm submitting this is because I added a
inject_with_index method to Array. I liked Daniel Martin's use of the
inject([0,0]), I didn't even think of doing that.

class Array
  def inject_with_index(injected)
    each_with_index{|obj, index| injected  ield(injected, obj, index) }
    injected
  end
end

class CreditCard
  @@types  
      'AMEX' { :start [34, 37], :length [15] },
      'Discover' { :start [6011], :length [16] },
      'MasterCard' { :start (51..55).to_a, :length [16] },
      'Vista' { :start [4], :length [13, 16] }
    }
  attr :type, true
  attr :valid, true
  def initialize(number)
    @type  @@types.find do |card_type, card_st|
      card_st[:start].any?{|st| /^#{st}/ number } and
        card_st[:length].any?{|le| number.length le }
    end || ['Unknown']).first
    @valid  umber.reverse.split('').inject_with_index(0) do |acc,num,ind|
        acc + ( ind%2 0 ? num.to_i :
            (num.to_i*2).to_s.split('').inject(0){|a,e|a.to_i+e.to_i} )
      end % 10 0
    puts " Type: #{@type}"
    puts "Valid: #{@valid}"
  end
end

------art_356970_20361627.1178026680671
Content-Type: application/x-ruby; name="credit_card.rb"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="credit_card.rb"
X-Attachment-Id: f_f16eo3es

IyEvdXNyL2Jpbi9ydWJ5CgpjbGFzcyBBcnJheQogIGRlZiBpbmplY3Rfd2l0aF9pbmRleChpbmpl
Y3RlZCkKICAgIGVhY2hfd2l0aF9pbmRleHt8b2JqLCBpbmRleHwgaW5qZWN0ZWQgPSB5aWVsZChp
bmplY3RlZCwgb2JqLCBpbmRleCkgfQogICAgaW5qZWN0ZWQKICBlbmQKZW5kCgpjbGFzcyBDcmVk
aXRDYXJkCiAgQEB0eXBlcyA9IHsKICAgICAgJ0FNRVgnID0+IHsgOnN0YXJ0ID0+IFszNCwgMzdd
LCA6bGVuZ3RoID0+IFsxNV0gfSwKICAgICAgJ0Rpc2NvdmVyJyA9PiB7IDpzdGFydCA9PiBbNjAx
MV0s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