Skip to content

Instantly share code, notes, and snippets.

@mdunsmuir
Created February 20, 2014 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdunsmuir/9121952 to your computer and use it in GitHub Desktop.
Save mdunsmuir/9121952 to your computer and use it in GitHub Desktop.
WIDTH = 80
puts $stdin.read.split(/\n\n+/).each_with_object([]) { |paragraph, result|
result << paragraph.split.each_with_object([""]) { |word, lines|
if (lines.last + " " + word).length > WIDTH
lines << word
elsif lines.last.empty?
lines[-1] += word
else
lines[-1] += (" " + word)
end
}.join(?\n)
}.join("\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment