Created
June 7, 2011 22:03
-
-
Save marvin/1013275 to your computer and use it in GitHub Desktop.
helper to truncate words
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 truncate_words(text, length, end_string = ' ...') | |
words = text.split() | |
words = words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this in a current sinatra project to cut the length of a task title that is shown in a task preview
use it like that
truncate_words("a very very long long title", 3, '...')
will cut it down to only show the first 3 words