def toll_free_dealer_phone(phone)
formated_phone = phone
area_code_supported = %w( 866 877 888)
area_code_supported.each do |area|
if phone.include?(area)
formated_phone = "1-#{phone}"
end
end
return formated_phone
end
Refactorings
No refactoring yet !
steved
July 20, 2009, July 20, 2009 20:05, permalink
def toll_free_dealer_phone(phone)
phone =~ /^(866|877|888)/ ? "1-#{phone}" : phone
end
andyjeffries
July 22, 2009, July 22, 2009 12:17, permalink
A simple gsub on the string will do it.
def toll_free_dealer_phone(phone) phone.gsub(/^(866|877|888)/, '1-\1') end
carlpelletier.myopenid.com
July 23, 2009, July 23, 2009 16:49, permalink
Thanks all, andyjeffries your code is not working. Dont known why? This code work:
def toll_free_dealer_phone(phone) phone.gsub(/(^.*)(866|877|888)/, '1-\1\2') end
Martin Plöger
July 27, 2009, July 27, 2009 13:39, permalink
carlpelletier.myopenid.com: Your code breaks when a number like "7888999" is entered => results in "1-7888999" which is wrong because it does not start with 888 (only contains it)
andyjeffries code works fine for me (although I would get rid of the braces... my opinion)
def toll_free_dealer_phone phone phone.gsub /^(866|877|888)/, '1-\1' end
Stephane Paul
October 19, 2009, October 19, 2009 18:33, permalink
how about getting the first match {1}
def toll_free_dealer_phone phone
phone.gsub(/^(866|877|888){1}/, "1#{$1}")
end
Pharmd145
October 28, 2009, October 28, 2009 09:24, permalink
Very nice site! cheap cialis http://opeyixa.com/qvoxaxa/4.html
Very nice site! cheap cialis http://opeyixa.com/qvoxaxa/4.html
best buys black friday sales
October 28, 2010, October 28, 2010 01:45, permalink
It is quite prominent to accept for befitting custody of all your jewellery pieces so that they mould for a lifetime. There are divers approaches and ways to straight distinct types of jewels be it gold, silver, pearls, diamond or marvel stones. Outlined under the sun are the several ways by which you can induce nurse of your accessories and maintain them gleaming and new always.
I need to build a helper method that will add a 1- in front of a toll free number if that number begin with 866, 877 or 888. This is my helper method. Wondering if you have better implementation?
thanks