Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save adammcarth/a5ef612dadcb8fdf3eee to your computer and use it in GitHub Desktop.

Select an option

Save adammcarth/a5ef612dadcb8fdf3eee to your computer and use it in GitHub Desktop.
# Intelligent string truncation function
# written in Ruby.
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(" ")
if truncated.length > max_chars
# Use standard truncation (set amount of characters)
truncated = string.slice(0, max_chars) + "..."
elsif truncated == string
# `truncated` and the original string are identical, so don't include "..."
truncated = string
else
# If everything's checked out, we'll be using worded truncation. This simply adds
# "..." to the worded truncation variable.
truncated = truncated + "..."
end
# Output the results
return truncated
end
truncate("I enjoy playing counter strike source.", 3)
#=> I enjoy playing...
truncate("I enjoy playing counter strike source.", 6)
#=> I enjoy playing counter strike sourc...
truncate("I enjoy playing counter strike source.", 100)
#=> I enjoy playing counter strike source.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment