Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change faker gem phone number format

Is there a way to control the format of the Phone number generated by faker?

When I call:

Faker::PhoneNumber.cell_phone.to_i

I end up getting the wrong value.

I also would like to not have extensions.

like image 877
rafamvc Avatar asked Jan 27 '14 17:01

rafamvc


2 Answers

You can set custom format on the fly like this:

Faker::Base.numerify('+90(###) ### ####')

This will solve your problem.

like image 132
zeitnot Avatar answered Oct 01 '22 13:10

zeitnot


Faker::PhoneNumber.cell_phone is basically just calling numerify with one of the predefined phone_number_formats.

So you could just use numerify with your own format. For e.g. If you want 10 digits number, you would do:

Faker.numerify('#########')

If you'd still like to use Faker::PhoneNumber.cell_phone but would like to get rid of the hyphens, you could use gsub to replace the hyphens as:

Faker::PhoneNumber.cell_phone.gsub(/-/, '')
like image 38
vee Avatar answered Oct 01 '22 14:10

vee