Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Created July 19, 2014 04:10
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 adammcarth/5f0cdb6ebc4d00ef1f70 to your computer and use it in GitHub Desktop.
Save adammcarth/5f0cdb6ebc4d00ef1f70 to your computer and use it in GitHub Desktop.
def truncate(string, words)
# Define the maximum number of characters that can be used before
# standard truncation is applied (prevents long words from ruinning HTML).
max_chars = 6 * words
words_array = string.split(" ") # splits each word into an array ["like", "this"]
index = words - 1 # array indexes start at 0, so let's take one number off the words amount.
truncated = words_array[0..index].join(" ")
return truncated + "..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment