April 29, 2007

Posted by John

Tagged gems and terminal

Older: Namespaces added to routes

Newer: Don't Reinvent The Wheel

Lorem ispum...

I have decided to start familiarizing myself with the Ruby standard lib and gems a bit more. I came across Gemtacular and started browsing through all the gems. I’m thinking I’ll pick out a gem each week, maybe more often, and give it some coverage here.

The first one that caught my eye was babel. babel was written by Ryan Davis (aka zenspider) and is basically a random name and word generator. It has several first, last and surnames inside it and provides a few methods for randomizing the output. It’s great for generating test data. Command line gems are my latest interest and, as such, I felt that was the only thing babel was missing.

The Idea

That’s when it hit me. What am I constantly doing every time I need some test data? If you said hitting up lipsum.com, you are correct. I have to open a browser (or new tab), go to lipsum.com, fill out the form, submit it and copy the text. I don’t know about you but that is too cumbersome. I broke out Dr. Nic’s newgem gem (which is fantastic) and an hour later lorem was created.

Installation

$ sudo gem install lorem

Usage

Lorem is a simple lib with a command line companion that makes it really easy to spit out dummy text. Below are the command line usage instructions.

Usage: lorem [number] [type] (ie: lorem 5 paragraphs)
- Number defaults to 1 if no parameters given
- Type defaults to paragraphs
- Available Types:
  - paragraphs
  - words
  - chars
  - characters

It can also be used in your ruby scripts like so:

require 'rubygems'
require 'lorem'

puts Lorem::Base.new('chars', 10).output
# => Lorem ipsu

puts Lorem::Base.new('words', 10).output
# => Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vitae

puts Lorem::Base.new('paragraphs', 1).output
# => Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
# Vivamus vitae risus vitae lorem iaculis placerat. Aliquam sit 
# amet felis. Etiam congue. Donec risus risus, pretium ac, tincidunt 
# eu, tempor eu, quam. Morbi blandit mollis magna. Suspendisse 
# eu tortor. Donec vitae felis nec ligula blandit rhoncus. Ut a pede 
# ac neque mattis facilisis. Nulla nunc ipsum, sodales vitae, 
# hendrerit non, imperdiet ac, ante. Morbi sit amet mi. Ut magna. 
# Curabitur id est. Nulla velit. Sed consectetuer sodales justo. 
# Aliquam dictum gravida libero. Sed eu turpis. Nunc id lorem. 
# Aenean consequat tempor mi. Phasellus in neque. Nunc 
# fermentum convallis ligula.

The Future

It would be handy to have it integrate with Active Record in development mode. I’m thinking you enter !lorem in a text area and before_validation it fills it with some default text. It could also work in text fields by typing !lorem 5 chars, etc. Thoughts?

I’m also thinking that I might add babel as a dependency and maybe make a command line companion for that as well. Then you can get babellipsum content all from your command line or in development mode of your rails app.

So what is the moral of the story? You don’t need a great idea to play with ruby or create a gem. You just need something that is laborious that ruby can simplify.

7 Comments

  1. Ha, great idea and execution. Thanks for this — it’ll come in very handy as I’ve been using lipsum.com for years and years.

  2. on the same note, if you use Textmate, try this snippet:
    lorem →tab

    lorem ipsum in your editor. pretty nice for quickly filling views.

  3. One word – GOLD! Should be a rails plugin as well.

  4. This is a great tool, but I find that I often need to create HTML. Now you can do this yourself using:

    Lorem::Base.new(‘paragraphs’, 3).output.split(%r{\n+}).map {|p| “

    ” + p + “

    ”}.join(“\n”)


    but it would be better if there was an output_html(wrapper) or output_wrapped that took a block where you could get the current item (paragraph, word or character) and the current index and wrap the result.

  5. @Adam – Good idea. I have a list of things I’d like to do with it when I get time. If you want to throw something together and send me a patch I’d be more than willing to look at it.

  6. Here’s a quick wrapper to randomize the strings produced
    Thanks for the useful gem!

    require ‘rubygems’
    require ‘lorem’

    1. http://www.railstips.org/2007/4/29/lorem-ispum
    2. lorem [number] [type] (ie: lorem 5 paragraphs/words/chars)

    class RandomText
    def initialize
    @lorem_unit = ‘words’
    @max_lorems = 2000

    1. load seed_text with a chank of static Lorem text
      seed_text = Lorem::Base.new(lorem_unit, @max_lorems).output

    end
    def output(max_output)
    out = @seed_text.split
    pos = rand(out.length – max_output)
    start_pos = (pos < 0 ? 0 : pos)
    end_pos = (pos + max_output > out.length ? out.length – max_output : pos + max_output)
    return out[start_pos…end_pos].join(" ")
    end

    end


    lorem = RandomText.new
    puts lorem.output(10).capitalize + “.”

  7. Oops… previous post got all messed up. Another try:

    Here’s a quick wrapper to randomize the strings produced Thanks for the useful gem!

    require ‘rubygems’
    require ‘lorem’

    1. http://www.railstips.org/2007/4/29/lorem-ispum
    2. lorem [number] [type] (ie: lorem 5 paragraphs/words/chars)

    class RandomText
    def initialize
    @lorem_unit = ‘words’
    @max_lorems = 2000

    1. load seed_text with a chank of static Lorem text
      seed_text = Lorem::Base.new(lorem_unit, @max_lorems).output

    end
    def output(max_output)
    out = @seed_text.split
    pos = rand(out.length – max_output)
    start_pos = (pos < 0 ? 0 : pos)
    end_pos = (pos + max_output > out.length ? out.length – max_output : pos + max_output)
    return out[start_pos…end_pos].join(" ")
    end

    end


    lorem = RandomText.new
    puts lorem.output(10).capitalize + “.”

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.

Projects

Flipper
Release your software more often with fewer problems.
Flip your features.