module CreditCards Types = { 13 => { /^4/ => 'Visa' }, 15 => { /^3[47]/ => 'American Express', /^2014|^2149/ => 'enRoute', /^2131|^1800/ => 'JCB' }, 14 => { /^30[0-5]|^3[68]/ => "Diner's Club", }, 16 => { /^3/ => 'JCB', /^4/ => 'Visa', /^5[1-5]/ =>'MasterCard', /^6011/ => 'Discover' } } def type_for_card(s) Types[s.length].each_pair do |r, t| return t if r =~ s end raise ArgumentError, "None found" end end