Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Ruby trim string by word count (See related posts)

def trim_by_words(string,wordcount)
    string.split[0..(wordcount-1)].join(" ") +(string.split.size > wordcount ? "..." : "")
end

Comments on this post

kazhar posts on Nov 25, 2008 at 08:13
Well that isn't really cutting by words. It doesn't removes dots, commas and others interrogation dots.

For example, if I write : Hey ! Ruby is fun.

You'll cut the exclamation dot as a word. But it isn't. You should first remove it.

You need to create an account or log in to post comments to this site.