Skip to content

Instantly share code, notes, and snippets.

@lucashungaro
Created December 13, 2010 14:44
Show Gist options
  • Save lucashungaro/739046 to your computer and use it in GitHub Desktop.
Save lucashungaro/739046 to your computer and use it in GitHub Desktop.
Smart truncate - won't "cut" words
def truncate(options = {})
options.reverse_merge!({:length => 50, :omission => "…"})
self.gsub(/^(.{#{options[:length]}}[\w.]*)(.*)/m) {$2.empty? ? $1 : $1 + options[:omission]}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment