Created
May 27, 2011 19:20
-
-
Save k2052/995939 to your computer and use it in GitHub Desktop.
Text Truncation By Word.
This file contains hidden or 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
class String | |
# Truncates a string | |
def truncate(length = 30, end_string = '…') | |
return self if self == nil | |
words = self.split() | |
words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment