Class: LoremIpsum
- Inherits:
-
Object
- Object
- LoremIpsum
- 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
-
.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.
Instance Method Summary collapse
-
#generate(paragraphs: 3, words_per_sentence: 10, seed: nil) ⇒ String
Generate a full Lorem Ipsum text.
-
#paragraph(sentences: 4) ⇒ String
Generate a paragraph of a given number of sentences.
-
#random_word ⇒ Object
Generate a random word from the pool.
-
#sentence(words: 8) ⇒ String
Generate a sentence of a given length (in words).
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.
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
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
53 54 55 |
# File 'spec/spec_helper.rb', line 53 def paragraph(sentences: 4) sentences.times.map { sentence }.join(' ') + "\n" end |
#random_word ⇒ Object
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)
46 47 48 |
# File 'spec/spec_helper.rb', line 46 def sentence(words: 8) words.times.map { random_word }.join(' ').capitalize + '.' end |