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? -- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Jul-05 02:52 UTC
[Rails] splitting a paragraph into words and spaces
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 On 7/4/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/b42ef87a/attachment.html
sean colquhoun
2006-Jul-05 04:11 UTC
[Rails] Re: splitting a paragraph into words and spaces
You are THE MAN!!! Works perfectly. Thanks. Mark Van Holstyn 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-- Posted via http://www.ruby-forum.com/.