Splitting a paragraph into words and spaces

I’m using this:

<% words = article.content.split(/ /) %>
<%= words[0…20] %>

to (ostensibly) split a paragraph into component words, with spaces in
between, then print to html only the first 20 items, words and spaces. I
got this (split(/ /)) from the online pickaxe book at
http://rubycentral.com/book/ref_c_string.html#String.split . The problem
is, the resulting array seems to contain no spaces, only words, and
what’s being printed to html is onehugewordthatlookslikethis.

Am I doing something wrong?

try this…

<% words = article.content.split(/ /) %>
<%= words[0…20].join ’ ’ %>

This will join the first 20 elements with spaces in between. When you
split,
it removes the text which you split on.

mark

You are THE MAN!!!
Works perfectly. Thanks.

Mark Van H. wrote:

try this…

<% words = article.content.split(/ /) %>
<%= words[0…20].join ’ ’ %>

This will join the first 20 elements with spaces in between. When you
split,
it removes the text which you split on.

mark

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs