Class: LoremIpsum

Inherits:
Object
  • Object
show all
Defined in:
spec/spec_helper.rb

Overview

A lightweight Lorem Ipsum generator.

The generator uses a small pool of classic Latin words and randomly assembles them into sentences and paragraphs.

Constant Summary collapse

WORDS =

Default word pool (classic Lorem Ipsum source)

%w[
  lorem ipsum dolor sit amet consectetur adipiscing elit sed do
  eiusmod tempor incididunt ut labore et dolore magna aliqua
  enim ad minim veniam quis nostrud exercitation ullamco
  laboris nisi ut aliquip ex ea commodo consequat duis aute
  irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur
  excepteur sint occaecat cupidatat non proident sunt
  culpa qui officia deserunt mollit anim id est laborum
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(paragraphs: 3, words_per_sentence: 10, seed: nil) ⇒ String

Generates Lorem Ipsum text with the specified number of paragraphs and average words per sentence.

Parameters:

  • paragraphs (Integer) (defaults to: 3)

    the number of paragraphs to generate (default: 3)

  • words_per_sentence (Integer) (defaults to: 10)

    the average number of words per sentence (default: 10)

Returns:

  • (String)

    the generated Lorem Ipsum text



22
23
24
# File 'spec/spec_helper.rb', line 22

def self.generate(paragraphs: 3, words_per_sentence: 10, seed: nil)
  new.generate(paragraphs:, words_per_sentence:, seed:)
end

Instance Method Details

#generate(paragraphs: 3, words_per_sentence: 10, seed: nil) ⇒ String

Generate a full Lorem Ipsum text

Parameters:

  • paragraphs (Integer) (defaults to: 3)

    number of paragraphs

  • words_per_sentence (Integer) (defaults to: 10)

    average words per sentence

Returns:

  • (String)

    the generated text



62
63
64
65
66
67
68
69
70
# File 'spec/spec_helper.rb', line 62

def generate(paragraphs: 3, words_per_sentence: 10, seed: nil)
  @random = seed ? Random.new(seed) : Random.new
  paragraphs.times.map do
    # For a bit of natural variation, pick a random number
    # of sentences around the requested average.
    sentences = @random.rand(words_per_sentence / 2..words_per_sentence * 2)
    paragraph(sentences: sentences)
  end.join("\n")
end

#paragraph(sentences: 4) ⇒ String

Generate a paragraph of a given number of sentences

Parameters:

  • sentences (Integer) (defaults to: 4)

    number of sentences in the paragraph

Returns:

  • (String)

    a paragraph ending with a newline



53
54
55
# File 'spec/spec_helper.rb', line 53

def paragraph(sentences: 4)
  sentences.times.map { sentence }.join(' ') + "\n"
end

#random_wordObject

Generate a random word from the pool



39
40
41
# File 'spec/spec_helper.rb', line 39

def random_word
  WORDS.sample random: @random
end

#sentence(words: 8) ⇒ String

Generate a sentence of a given length (in words)

Parameters:

  • words (Integer) (defaults to: 8)

    number of words in the sentence

Returns:

  • (String)

    a sentence ending with a period



46
47
48
# File 'spec/spec_helper.rb', line 46

def sentence(words: 8)
  words.times.map { random_word }.join(' ').capitalize + '.'
end