Skip to content

Instantly share code, notes, and snippets.

@k2052
Created May 27, 2011 19:20
Show Gist options
  • Save k2052/995939 to your computer and use it in GitHub Desktop.
Save k2052/995939 to your computer and use it in GitHub Desktop.
Text Truncation By Word.
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