Project

madlibs

0.0
No commit activity in last 3 years
No release in over 3 years
Because sometimes, you just need to make something stupid.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.3
>= 0

Runtime

 Project Readme

Madlibs

This gem simply takes a template, does some randomizing, applies a dictionary, and produces a string. Depending on the dictionary, it can be quite humorous.

Usage

The Dictionary

The dictionary is a simple ruby hash with keyed arrays of words, but there are a few rules.

  1. Make sure your keys are plural

Ok, so maybe there is just that one.

The Template

The template has 3 main syntax entities

Keys

Very simply, <key> signifies it to do a random replace from the correlated dictionary, the name of the key is arbitrary as long as it is singular in the template and plural in the dictionary.

For example <key> would match the keys dictionary hash key.

You are ensured that you won't get the same word twice in a single generation.

Optionals

Wrapping any segment of text in parenthesis immediately followed by a ? will make the generation randomly include or exclude the inner text. And yes, they can be nested.

"This is a(n example)? template."

Will result in either

"This is a template."

or

"This is an example template."

Word Lists

Lastly, for another element of randomness, you can provide a word list that it will choose from at random.

"This is a (great|terrible) example."

Will result in either

"This is a great example."

or

"This is a terrible example."

This can be useful for creating more entropy from a set of common small interchangeable words.

"(the|a|some|one)"

Full Example

require 'madlibs'

# Create a new Madlib object, passing a template and a dictionary.  You can also
# pass nothing and assign them after the fact with `.dictionary` and `.template`
madlib = Madlibs::Madlib.new(
  'I will <verb> (the|another)( <adjective>)? <noun>',
  {
    "nouns" => [
      "proxy",
      "cache",
      "architecture",
      "fragmentation",
    ],
    "verbs" => [
      "refactor",
      "implement",
      "replicate",
      "debug",
    ],
    "adjectives" => [
      "distributed",
      "imperative",
      "virtualized",
    ]
  }
)

# Generate your string(s)

madlib.generate
# => "I will debug the architecture"

madlib.generate
# => "I will implement another imperative cache"

madlib.generate
# => "I will refactor another virtualized proxy"

Installation

Add this line to your application's Gemfile:

gem 'madlibs'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install madlibs

Development

rake test
rake build
rake install