def trim_by_words(string,wordcount) string.split[0..(wordcount-1)].join(" ") +(string.split.size > wordcount ? "..." : "") end
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)
Comments on this post
You need to create an account or log in to post comments to this site.
Related Posts
» Typo under lighttpd in lighttpd ruby rails typo ruby-fcgi
» password hash script in ruby password
» Stable sort in ruby sort
» date time in ruby in ruby time date
» rip calendar attachments fro... in email ruby calendar
» count characters in ruby count
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails
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.