Faker Gem - A Quick and Dirty Introduction

One of the first ruby gems I used was the Faker Gem, a nice little ruby gem that...wait for it... Fakes Data!

Usage is simple; first grab the gem using your gem program:

$sudo gem install faker  

Now you can use it in your favorite ruby program or script. For now let's fire up irb and see what we can fake. 

require 'rubygems'

require 'faker'

#Let's see if we can make a fake person...

person_name = Faker::Name.name

person_email = Faker::Internet.email

person_company = Faker::Company.name

person_company_slogan = Faker::Company.bs

person_phone = Faker::PhoneNumber.phone_number

person_address = Faker::Address.street_address

person_city = Faker::Address.city

person_state = Faker::Address.us_state

person_zip = Faker::Address.zip_code

# Wowee-wow-wow. Now let's print him off some business cards

puts "#{person_name}

#{person_company}

'#{person_company_slogan.capitalize}'

#{person_address}

#{person_city}, #{person_state} #{person_zip}

Call me at: #{person_phone}

Email me at: #{person_email}

It's a pleasure doing business."

# Awesome, now we can go into business and

# get back our money from that Nigerian prince fellow...

 

As you can see Faker is pretty easy to use; you can build some powerful test bases using it inside a loop:

require 'rubygems'

require 'faker'

require 'activerecord'

1000.times do

  # make a person

  # save person

end

For more information see the Faker Docs:

In my next post I will show you how you can use all this fake data you're generating to test anything you need data for.

--Alex

This article is part of the GWB Archives. Original Author: Alex Moore

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.