Skip to content

Instantly share code, notes, and snippets.

@dlt
Created March 14, 2011 15:52
Show Gist options
  • Save dlt/869353 to your computer and use it in GitHub Desktop.
Save dlt/869353 to your computer and use it in GitHub Desktop.
def word_wrap(word, len = 22)
if word.size < len
word
else
new_word = []
parts = word.split(/\s/).map { |p| p.strip }
while part = parts.shift and (new_word.join(" ").size + part.size) < len
new_word << part
end
new_word.join(" ") + "..."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment