Created
March 14, 2011 15:52
-
-
Save dlt/869353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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