15

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.

rafamvc
  • 8,227
  • 6
  • 31
  • 34

2 Answers2

28

You can set custom format on the fly like this:

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

This will solve your problem.

zeitnot
  • 1,304
  • 12
  • 28
7

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(/-/, '')
vee
  • 38,255
  • 7
  • 74
  • 78