Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
# Usage: tweetsplit.rb < tweetstorm.txt
# https://twitter.com/CoralineAda/status/869204799027372032
# Sam Stephenson / 2017-05-29
# Tweet-sized version:
# puts $<.read.split(/\s+/).reduce(nil){|(*cs,c),w|n=[c,w].compact*" ";cs+(n.size>140?[c,w]:[n])}*"\n"
def chunks_for(words, chunk_size)
words.reduce([nil]) do |(*chunks, chunk), word|
chunks + next_chunk(chunk_size, chunk, word)
end
end
def next_chunk(chunk_size, chunk, word)
candidate = [chunk, word].compact.join(" ")
if candidate.size > chunk_size
[chunk, word]
else
[candidate]
end
end
puts chunks_for($<.read.split(/\s+/), 140).join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment