Using Faker in Ruby

Edward Acosta
3 min readSep 30, 2020

Be sure to check out my other work on LinkedIn and Github

Faker Logo
Faker Logo

If you’ve ever found yourself in need of fake data for testing code, learning Faker is a must to speed up your production. In this quick guide I’ll be showing you the basics of using this Ruby Gem to save time by quickly generating large amounts of data.

Photo by Chris Ried on Unsplash

One of the challenges of starting a new project is the inherent lack of data that comes with it, and although you could spend hours writing line after line after line of code, spending less time to get more stuff done is paradigm to programming. Enter Faker, a Ruby gem that is a port of Data::Faker, a Perl extension. This has an enormous library of ever expanding fake data that is easy to access and use.

Getting started with the gem is easy, all you need to do is install it by using ‘gem install faker’ in your terminal, adding it to your gemfile, and requiring it wherever you need fake data. You can see its source code here. The libraries of data range from wacky to incredibly useful. Once the gem is installed, using it is easy.

You can create almost anything, from creating ID numbers, zip codes, street addresses, quotes from famous movies, lorem ipsum, DnD classes, random alphanumeric numbers, and more. But Faker also lets you fine tune the results that it returns by using arguments in some of the requests.

Faker::Number.between(from: 1, to: 10) #=> 7
Faker::Number.decimal(l_digits: 2, r_digits: 3) #=> 83.843

You’re able to get data that works for exactly what you need it to.

Even better if you’re unsure of what kind of fake data you need, there is a Faker Bot that will allow you to search through the Faker library to find exactly what is applicable to your situation. Installing is just as easy as Faker, run ‘gem install faker-bot’ and afterwards you’re able to perform lookups in the terminal by using ‘faker search’ followed by your search term. The flag ‘-v’ will return verbose results, that have an example of the type of data that is returned

And if you just want to browse through the Faker library, Faker bot has that covered too! Just put ‘faker list -v’ in your terminal, and that will return a complete list of the faker items with examples, making it easy for you to find the exact fake data you need.

In the event that faker doesn’t have the generator that you need, you are able to customize the results you’ll get back by editing the gem inside of the lib/locales folder.

en-au-ocker:
faker:
name:
# Existing faker field, new data
first_name:
- Charlotte
- Ava
- Chloe
- Emily

# New faker fields
ocker_first_name:
- Bazza
- Bluey
- Davo
- Johno
- Shano
- Shazza
region:
- South East Queensland
- Wide Bay Burnett
- Margaret River
- Port Pirie
- Gippsland
- Elizabeth
- Barossaee

Using Faker to seed databases is one of the biggest time savers I’ve learned so far, and I hope that you are able to use this quick guide to help you speed up development.

--

--

Edward Acosta

Student at Flatiron School for software engineering. Graduating in December