321d08dbad2770da0994b5f53bc8757a

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

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 !

D41d8cd98f00b204e9800998ecf8427e

steved

July 20, 2009, July 20, 2009 20:05, permalink

No rating. Login to rate!
def toll_free_dealer_phone(phone)
  phone =~ /^(866|877|888)/ ? "1-#{phone}" : phone
end
5cb7a05b6de4a6655319db9333be429c

andyjeffries

July 22, 2009, July 22, 2009 12:17, permalink

No rating. Login to rate!

A simple gsub on the string will do it.

def toll_free_dealer_phone(phone)
  phone.gsub(/^(866|877|888)/, '1-\1')
end
321d08dbad2770da0994b5f53bc8757a

carlpelletier.myopenid.com

July 23, 2009, July 23, 2009 16:49, permalink

No rating. Login to rate!

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
E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

July 27, 2009, July 27, 2009 13:39, permalink

No rating. Login to rate!

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
A486c31326dffba0cde8bade19108dc2

Stephane Paul

October 19, 2009, October 19, 2009 18:33, permalink

No rating. Login to rate!

how about getting the first match {1}

def toll_free_dealer_phone phone
   phone.gsub(/^(866|877|888){1}/, "1#{$1}")
end
Dd561c3cdece242a4142110d48cd22f5

Pharmd145

October 28, 2009, October 28, 2009 09:24, permalink

No rating. Login to rate!

Very nice site! cheap cialis http://opeyixa.com/qvoxaxa/4.html

Very nice site! cheap cialis http://opeyixa.com/qvoxaxa/4.html
24a7fd0a6184ff8f17530410ee900e3e

Pharmd382

November 1, 2009, November 01, 2009 06:38, permalink

No rating. Login to rate!

Very nice site!

Very nice site!
6f59481d45ae9cd6efb124e3c1af78fd

Pharme395

November 8, 2009, November 08, 2009 04:03, permalink

No rating. Login to rate!

Very nice site!

Very nice site!
5c7c9a416f10e48c1299ba9c671276c3

QtwgJjLh

November 18, 2009, November 18, 2009 22:32, permalink

No rating. Login to rate!

Hi! EeKEDXR

Hi! EeKEDXR
8cf7ea87d104d4b573c6194090e6294c

BGIqyl

November 19, 2009, November 19, 2009 20:15, permalink

No rating. Login to rate!

Hi! EMzmXA

Hi! EMzmXA
502037aa64cf9ad19591b13a88b69f7b

hBQoXw

November 20, 2009, November 20, 2009 19:35, permalink

No rating. Login to rate!

Hi! GCpYSI

Hi! GCpYSI
2d11f97544405698f8fab9aacb92e614

JgeOMto

December 22, 2009, December 22, 2009 08:02, permalink

No rating. Login to rate!

Hi! DVSITht

Hi! DVSITht
Dc6ae4723f1046f81ff2c348c9b54b55

IhJcQIxZ

December 22, 2009, December 22, 2009 16:32, permalink

No rating. Login to rate!

Hi! FjXoosiG

Hi! FjXoosiG
46156591e78387e09eda36de2f384349

best buys black friday sales

October 28, 2010, October 28, 2010 01:45, permalink

No rating. Login to rate!

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.

Your refactoring





Format Copy from initial code

or Cancel