sevenwire/forgery — pull request #70 by mouchtaris (2014-12-02 19:42:41+00:00)
Title: Proposition for LoremIpsum#paragraphs()

I have made LoremIpsum#parahraph() yield each paragraph, if a block is given. Otherwise it behaves exactly as before (returns a string of paragraphs). All the options that would be taken into account when returning a string are also honored when yielding paragraphs, **except for** *separator*.

For instance

Forgery(:lorem_ipsum).paragraphs(2) do |paragraph| puts paragraph end

would print each paragraph.

Of course, for a more coherent API, the same feature should be there with every other method of lorem ipsum (and perhaps other generators as well).

Also, I am not sure how the *separator* option should be taken into account when using the *yield* feature of the method, but my opinion is that the caller can use their separator inside the block they provide directly.

Finally, if I had to propose a cleaner API reform, I would factor out the generation code into *each_\** methods, and make normal methods be simple wrappers for their *each_* corresponding ones. For example:

def each_paragraph(number_of_paragraphs)
return enum_for(:each_paragraph, number_of_paragraphs) unless block_given?
number_of_paragraphs.times do
yield generate_paragraph
end
end

def paragraphs(num, opts)
each_paragraph(num).to_a.join(opts[:separator])
end

If people find this an interesting idea, I could continue this idea in some future commits.