How To Generate Fake Names, Addresses And More In Ruby

by Mr How on November 9, 2008 · 1 comment

Sometimes it is useful to be able to generate “fake data” on the fly. Perhaps you need to populate a database, supply some “dummy” data to a user, or test how your app deals with a wide range of input. This How will show you how.

Prerequisites

We’re going to be using the faker gem, developed by Benjamin Curtis. It’s distributed as a RubyGem, so installation is easy:

gem install faker

Note! Refer to our How To Install A Ruby Gem page if you need to learn more about installing gems. For example, you may need to prefix the above with sudo

Faker has no prerequisites so it’s a very easy install.

Fake Names

To generate fake data, you call class methods on the Faker class. For example:

10.times do
  puts Faker::Name.name
end

The results:

Margarita Nader
Terrill Marks
Ms. Vivianne Robel
Mr. Josefina Ebert
Christine Graham
Dusty Lang
Coby Zboncak Sr.
Dana Kerluke IV
Camille Doyle
Jaida Ebert

Fake E-mail Addresses

5.times do
  puts Faker::Internet.email
end

The results:

dewitt.hahn@oconnell.us
jermain@mayer.info
aiden_block@ratke.biz
elvis.mcglynn@osinski.biz
martin@grant.name

Don’t think you can just randomly spam any e-mail addresses created by Faker. They’re very unlikely to exist!

Fake Company Names and Slogans

These are priceless! For example:

puts Faker::Company.name
puts Faker::Company.catch_phrase
puts Faker::Company.bs

The results:

Tillman, O'Keefe and Lockman
Distributed full-range policy
transform frictionless content

I’ll let you guess what the bs method stands for!

Fake Street Addresses

The address features are quite indepth:

puts Faker::Address.city
puts Faker::Address.uk_county
puts Faker::Address.zip_code
puts Faker::Address.street_address
puts Faker::Address.us_state

The results:

Mooreview
Hampshire
83292
2502 Rosemary Fort
Wisconsin

Dummy Text / Lorem Ipsum

As well as fake identity creation, you can also create dummy text in paragraphs, sentences, or words:

puts Faker::Lorem.paragraphs(2)

The result:

Temporibus minima et ipsa ea exercitationem. Commodi et tenetur reiciendis et. Accusantium
ipsum quia quisquam molestiae sed. Ut minus reiciendis ad nihil. Neque et debitis qui
voluptates deleniti autem et vel.
Repellat atque quibusdam eum eaque. Laboriosam ut ipsam possimus cupiditate. Numquam at
maiores aliquid et minima et optio. Omnis cupiditate velit dolor quo esse architecto.

Or:

puts Faker::Lorem.sentences(2)

The result:

Aut adipisci temporibus repellendus dignissimos quia reiciendis.
Atque alias adipisci ex possimus ipsum non magnam.

Further Reading

{ 1 trackback }

3 Ways To Build Fake Demo Data For Your Rails App
11.14.08 at 8:13 am

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>