Skip to content

Instantly share code, notes, and snippets.

@dramsay
Created September 26, 2008 14:09
Show Gist options
  • Save dramsay/13106 to your computer and use it in GitHub Desktop.
Save dramsay/13106 to your computer and use it in GitHub Desktop.
def truncate_words(text, length=30, end_string="...")
pattern = %r(^(.{#{length}}[\w.]*)(.*))
text.gsub(pattern) {$2.empty? ? $1 : $1 + end_string}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment