Created
July 19, 2014 04:10
-
-
Save adammcarth/5f0cdb6ebc4d00ef1f70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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