Skip to content

Instantly share code, notes, and snippets.

@timnovinger
Created January 21, 2010 03:04
Show Gist options
  • Save timnovinger/282544 to your computer and use it in GitHub Desktop.
Save timnovinger/282544 to your computer and use it in GitHub Desktop.
# TRUNCATE BY WORD COUNT
def snippet (text, wordcount, separator="...")
text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? separator : "")
end
## EXAMPLE USAGE
<%= snippet(p.content, 20, "... <a href='/notes/#{p.slug}'>read more</a>") %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment