From: Erik Veenstra Date: 2005-01-05T15:21:32+09:00 Subject: Re: what's the Ruby way to do this? On Wed, 05 Jan 2005 15:11:15 +0900, Miles Keaton wrote: > What's the Ruby way to do this? > If I want to get the first 10 words from a phrase, followed by "..."? > Although the following works, I can tell it's just not Rubyish: > > def snippet(thought) > result = '' > i = 0 > thought.split.each do |word| > if i < 10 > result = (result + ' ' + word) > end > i = (i + 1) > end > result = result.strip + '...' > return result > end def snippet(thought) thought.split[0..9].join(" ") + "..." end gegroet, Erik V.