In my app, a Story has a summary field, and on the page that lists
stories, i want to show the first ‘n’ (eg twenty) words from the summary
(which i would follow with “…”).
Does anyone know a simple way to do this? I can think of complicated
and ugly ways to do it, involving going through the string a char at a
time counting spaces, but i’m guessing there’s a neat rails helper
(since there usually is!).
In my app, a Story has a summary field, and on the page that lists
stories, i want to show the first ‘n’ (eg twenty) words from the
summary
(which i would follow with “…”).
Does anyone know a simple way to do this? I can think of complicated
and ugly ways to do it, involving going through the string a char at a
time counting spaces, but i’m guessing there’s a neat rails helper
(since there usually is!).
I usually go for the roll-your-own method, something like this should
work nicely for you, just pop the method in your application_helper
and call it in your views.
def first_x_words(str,n=20,finish=‘…’)
str.split(’ ')[0,n].inject{|sum,word| sum + ’ ’ + word} + finish
end
my_string = ‘Praesent luptatum zzril delenit augue duis dolore te
feugait nulla facilisi nam! Modo typi qui nunc nobis videntur parum
clari fiant sollemnes?’
p first_x_words(my_string) # => “Praesent luptatum zzril delenit
augue duis dolore te feugait nulla facilisi nam! Modo typi qui nunc
nobis videntur parum clari…”
First of all, split takes a limiter, and its inverse is join:
words = str.split(’ ‘, n + 1)
words[-1] = finish
words.join(’ ')
But is this really what you want? Suppose the book was a discussion
on Mary Poppins? It seems to me that you have space for n characters,
and you need to end at a word break:
letters = str[0…limit - finish.length]
letters.sub!(/ \S*$/)
letters + " " + finish
def first_x_words(str,n=20,finish=’…’)
str.split(’ ')[0,n].inject{|sum,word| sum + ’ ’ + word} + finish
end
That’s perfect, thanks Douglas.
(i would never think to use inject as it still confuses me a little!)
No need for inject–you can rewrite that line:
str.split(’ ‘)[0,n].join(’ ') + finish
If, like another poster said, it is a CHARACTER limitation, yet you
still want to break on a word boundary, then replace the line with:
str[0,n].sub(/ ?\S*$/,’’) + finish
First of all, split takes a limiter, and its inverse is join:
words = str.split(’ ‘, n + 1)
words[-1] = finish
words.join(’ ')
But is this really what you want? Suppose the book was a discussion
on Mary Poppins? It seems to me that you have space for n characters,
and you need to end at a word break:
letters = str[0…limit - finish.length]
letters.sub!(/ \S*$/)
letters + " " + finish
Your suggestion is actually closer to what i want - thanks! However,
i’m having problems with the regular expression is sub - shouldn’t sub
take two parameters? What exactly is this line meant to be doing?
I put the above into a helper method, and when i call it i get a strange
error:
“wrong number of arguments (1 for 2)”
I’m definitely calling it correctly, and when i take the .sub line out
then it works (except for completing the last word, obviously).
In my app, a Story has a summary field, and on the page that lists
stories, i want to show the first ‘n’ (eg twenty) words from the summary
(which i would follow with “…”).
Does anyone know a simple way to do this? I can think of complicated
and ugly ways to do it, involving going through the string a char at a
time counting spaces, but i’m guessing there’s a neat rails helper
(since there usually is!).
thanks
max
Hey Max,
do you not just want the truncate rails helper?
Basically the same thing aside from the encapsulation in a class.
Didn’t see that one when I wrote mine. I went ahead and pluginized
mine, though, since I’ll probaby use it often: