Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/phone-numbers/ruby$ _

Generate Random Phone Numbers in Ruby

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Ruby

What: Generate random phone numbers in Ruby using rand() and Faker gem. Covers US format, international format, and validation for Rails testing.

Try it: Use our interactive Phone-numbers generator or integrate this code into your Ruby application.

Generate random phone numbers in Ruby using rand() and Faker gem. Covers US format, international format, and validation for Rails testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Go or use our interactive web generator.

Ruby Code Example

require "securerandom"

# Generate random US phone number (xxx) xxx-xxxx
def random_us_phone_number
  area_code = rand(200..999)
  exchange = rand(200..999)
  line_number = rand(1000..9999)
  format("(%03d) %03d-%04d", area_code, exchange, line_number)
end

# Generate random phone number with country code
def random_international_phone(country_code = "+1")
  area_code = rand(200..999)
  exchange = rand(200..999)
  line_number = rand(1000..9999)
  format("%s (%03d) %03d-%04d", country_code, area_code, exchange, line_number)
end

# Generate phone number without formatting
def random_phone_digits(length = 10)
  length.times.map { rand(0..9) }.join
end

# Validate US phone number format
def valid_us_phone?(phone)
  phone.match?(/^\(\d{3}\) \d{3}-\d{4}$/)
end

# Generate multiple phone numbers
def generate_phone_numbers(count = 10)
  Array.new(count) { random_us_phone_number }
end

# Using Faker gem (gem install faker)
# require "faker"
# phone = Faker::PhoneNumber.phone_number         # (555) 123-4567
# phone_e164 = Faker::PhoneNumber.phone_number_with_country_code  # +1 555-123-4567
# cell = Faker::PhoneNumber.cell_phone            # 555-123-4567

puts "US Phone: #{random_us_phone_number}"
puts "International: #{random_international_phone(\"+44\")}"
puts "Digits only: #{random_phone_digits(10)}"
puts "Valid: #{valid_us_phone?(\"(555) 123-4567\")}"
puts "Batch: #{generate_phone_numbers(3).join(\", \")}\"

[EXPLANATION]

Ruby uses rand(200..999) for generating area codes, exchanges, and line numbers. format("%03d") or sprintf formats with leading zeros. String#match? validates regex patterns efficiently. Array.new(count) { block } creates arrays with generated values. Faker gem offers PhoneNumber.phone_number for various formats and phone_number_with_country_code for international format. Ranges and blocks provide clean, idiomatic Ruby code.

Expected Output

US Phone: (555) 234-5678
International: +44 (555) 234-5678
Digits only: 5551234567
Valid: true
Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123

Common Use Cases

  • Generate phone numbers for Rails user registration testing
  • Create contact data for ActiveRecord model testing
  • Populate test databases with FactoryBot and Faker
  • Test phone validation in Ruby on Rails applications
  • Generate mock data for SMS notification systems

Important Notes

  • format("%03d") or sprintf("%03d") pad with leading zeros
  • Faker gem integrates seamlessly with RSpec and FactoryBot
  • match? is more efficient than match for boolean checks
  • Ranges (200..999) are inclusive on both ends in Ruby
  • Array.new(n) { block } executes block n times for initialization

Try Our Interactive Generator

Don't want to write code? Use our free web-based Phone-numbers generator with instant results.

TRY PHONE-NUMBERS GENERATOR →