Faker Ruby Library

repository·main·Indexed 10 days ago

https://github.com/faker-ruby/faker

A Ruby library used to generate realistic fake data—including names, addresses, internet credentials, and blockchain addresses—for testing, demos, and database population. It supports over 40 locales via the I18n gem and provides specialized generators for books, creatures, and blockchain data (e.g., Bitcoin, Ethereum, Tezos).

Tokens
82K
Snippets
413
Records
425
Agent score
92%

What's inside Faker

  1. Explore Faker generators by use case

    main

    Faker provides a wide array of generators organized by functional categories. You can find specific generators for common data needs such as:

    • Internet: Emails, passwords, domains, etc. (Faker::Internet, Faker::Email, etc.)
    • Date and Time: Past and future dates/times (Faker::Date, Faker::Time)
    • Person: Names, genders, jobs, and bios (Faker::Name, Faker::Job, Faker::Gender)
    • Number and String: Random numbers, barcodes, and text (Faker::Number, Faker::String, Faker::Lorem)
    • Location: Addresses, countries, and travel locations (Faker::Address, Faker::Nation, Faker::Travel::Airport)
    • Finance: Banking, crypto, and commerce (Faker::Bank, Faker::Blockchain::Bitcoin, Faker::Commerce)
    • Image: Avatars and placeholder images (Faker::Avatar, Faker::LoremFlickr)
    • Food: Coffee, beer, and restaurant data (Faker::Coffee, Faker::Restaurant)
    • Animal: Breeds and names (Faker::Creature::Dog, Faker::Creature::Cat)
    • Arts and Media: Movies, music, books, and TV shows (Faker::Movie, Faker::Music, Faker::Book, Faker::TvShows::BreakingBad)

    Each generator is accessed via its corresponding module in the Faker namespace.

  2. Explore available Faker generators by category

    main

    Faker provides a vast array of generators organized into namespaces. You can find specific data generators categorized by themes such as:

    • Default: Core generators like Faker::Address, Faker::Name, Faker::Internet, Faker::Finance, and Faker::Lorem.
    • Blockchain: Specialized generators for Bitcoin, Ethereum, Tezos, and Aeternity.
    • Books: Generators for specific series like Dune, Lovecraft, or The Culture Series.
    • Creature: Animal-specific generators including Faker::Creature::Dog, Cat, Bird, etc.
    • Games: Extensive support for various game universes like Minecraft, Pokemon, Zelda, and WorldOfWarcraft.
    • Movies & TV Shows: Data from specific franchises like Star Wars, Harry Potter, Breaking Bad, and Friends.
    • Music & Quotes: Generators for specific artists (e.g., Prince, Pearl Jam) or famous quotes (e.g., Shakespeare).

    Each generator is accessed via its corresponding namespace (e.g., Faker::Namespace::Class.method).

  3. Generate markdown formatting with Faker::Markdown

    main

    The Faker::Markdown module (available since version 1.8.0) generates various markdown-formatted strings using Lorem Ipsum text. It can produce headers, lists, code blocks, tables, and complex text structures.

    # Example of generating a random header
    Faker::Markdown.headers
  4. Generate random HTML content with Faker::HTML

    main

    The Faker::HTML module (available since version 3.2.1) provides various methods to generate random HTML snippets, ranging from simple tags to complex structures like tables and lists.

    # Example of using the module
    Faker::HTML.heading
    Faker::HTML.paragraph
  5. How to ensure unique values

    main

    By default, Faker does not guarantee unique values. To ensure a generator returns a value that hasn't been used in the current session, prefix the method call with .unique.

    Note: If you exhaust the pool of available unique values for a specific generator, Faker will raise a Faker::UniqueGenerator::RetryLimitExceeded exception.

    To prevent collisions or reset the uniqueness tracker (e.g., between test runs), you can clear the used values:

    • Faker::Name.unique.clear: Clears used values for a specific generator.
    • Faker::UniqueGenerator.clear: Clears used values for all generators.
    # Return a unique name every time
    Faker::Name.unique.name
    
    # Clear used values for Faker::Name
    Faker::Name.unique.clear
    
    # Clear used values for all generators
    Faker::UniqueGenerator.clear
  6. Identify when a generator was added using @faker.version

    main

    Each generator implementation contains a @faker.version tag in its source code. You can use this tag to determine the minimum version of the Faker gem required to access that specific method or class. This is useful for debugging compatibility issues or determining if you need to update your gem version.

    # Example of how a version tag appears in the source code
    
    # Faker::TvShows::ParksAndRec.character
    
    # @faker.version 1.9.0
    def character
      fetch('parks_and_rec.characters')
    end
  7. How English locale files are structured

    main

    In the faker-ruby/faker project, the English locale data is split across multiple files in the lib/locales/en/ directory to prevent a single en.yml file from becoming too large.

    Each file in this directory corresponds to a specific Faker class. For example, the data used by Faker::Internet is stored in internet.yml within this directory.

  8. How deterministic random works in Faker

    main

    Faker allows you to seed its pseudo-random number generator (PRNG) to produce the same sequence of data every time. This is useful for reproducible tests. You can set the seed by assigning a new Random object to Faker::Config.random.

    # Set a seed for deterministic output
    Faker::Config.random = Random.new(42)
    Faker::Lorem.word              #=> "velit"
    Faker::Lorem.word              #=> "quisquam"
    
    # Re-seeding with the same value produces the same sequence
    Faker::Config.random = Random.new(42)
    Faker::Lorem.word              #=> "velit"
    
    # Reset to default entropy sources
    Faker::Config.random = nil
  9. Generate localized ID numbers using Faker::Config.locale

    main

    The Faker::IdNumber.valid and Faker::IdNumber.invalid methods are locale-aware. By setting Faker::Config.locale, these generic methods will return valid or invalid identification numbers specific to that region.

    For example, setting the locale to en-GB will cause .valid to return a British National Insurance number.

    Faker::Config.locale = 'fr-FR'
    Faker::IdNumber.valid #=> "22510589696868"
  10. Generate World of Warcraft data with Faker::Games::WorldOfWarcraft

    main

    Use the Faker::Games::WorldOfWarcraft module to generate randomized data related to the World of Warcraft universe, including hero names, character classes, races, and quotes.

    # Generate a random character from World of Warcraft
    Faker::Games::WorldOfWarcraft.hero #=> "Uther the Lightbringer"
    
    # Generate a random class name from World of Warcraft
    Faker::Games::WorldOfWarcraft.class_name #=> "Druid"
    
    # Generate a random race from World of Warcraft
    Faker::Games::WorldOfWarcraft.race #=> "Night elf"
    
    # Generate a random quote from World of Warcraft
    Faker::Games::WorldOfWarcraft.quote #=> "These are dark times indeed."
  11. Generate Harry Potter themed data with Faker::Movies::HarryPotter

    main

    Use the Faker::Movies::HarryPotter module to generate random data related to the Harry Potter franchise, including characters, locations, quotes, books, houses, and spells. This module has been available since version 1.7.3.

    Faker::Movies::HarryPotter.character #=> "Harry Potter"
    Faker::Movies::HarryPotter.location #=> "Hogwarts"
    Faker::Movies::HarryPotter.quote #=> "I solemnly swear that I am up to no good."
    Faker::Movies::HarryPotter.book #=> "Harry Potter and the Chamber of Secrets"
    Faker::Movies::HarryPotter.house #=> "Gryffindor"
    Faker::Movies::HarryPotter.spell #=> "Reparo"
  12. Use Faker::TvShows::Simpsons to generate Simpsons data

    main

    The Faker::TvShows::Simpsons module allows you to generate random data related to The Simpsons TV show, including characters, locations, and quotes. This module has been available since version 1.8.0.

    Faker::TvShows::Simpsons.character #=> "Charles Montgomery Burns"
    
    Faker::TvShows::Simpsons.location  #=> "Moe's Tavern"
    
    Faker::TvShows::Simpsons.quote     #=> "It takes two to lie: one to lie and one to listen."