Skip to content

Instantly share code, notes, and snippets.

@Epyon616
Created November 22, 2010 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epyon616/709741 to your computer and use it in GitHub Desktop.
Save Epyon616/709741 to your computer and use it in GitHub Desktop.
a quick way of truncating text in Ruby
def truncate_words(text, url, length = 120, end_string = "... <a href='#{url}' class='more'>read more <img src='/images/black/arrow_right_8x8.png' /></a>")
words = text.split()
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