On Sat, 8 Jan 2005 05:06:27 +0900, William James <w_a_x_man / yahoo.com> wrote:

> <snip scathing sarcasm>  Glad to meet you, Belorian.

owch. Did that raise you up a notch?

> def snippet(thought)
> thought.match(/(\S+(\s+|$)){0,10}/)[0]+($'>""?"...":"")
> end
> 
> It's really quite straightforward.  Broken down:

Though I feel I am at least somewhat experienced in the regexp way, I
still balked a little when I saw that... yes, I could read it, but it
was not nearly as easy to read as the ruby code. There was altogether
too much punctuation on that line for my taste :) Still, using a
regexp has the desirable attribute of being non-lossy, while the
split-join method does not. Here's a slightly simplified example that
makes use of #sub to do the dirty work:

def snippet(thought)
  thought.sub(/((?:\S+\s+){10}).+/) {$1 + "..."}
end

t = "It is better to have loved and lost, than never to have loved at
all." # size: 15 words
u = "To be or not to be, that is the question." # size: 10 words

snippet t
    ==>"It is better to have loved and lost, than never ..."
snippet u
    ==>"To be or not to be, that is the question."

In this one, #sub tries to replace the expression, but it fails if
there are less than ten words, returning the unmodified string.
Otherwise, the first ten words are captured, then appended with
ellipses. The ellipses are not added on the second quote, since it
just squeaks in under the ten word limit. (This could be modified to
eliminate the trailing space, but I didn't want to bother with that
just now :)


A #split solution that behaves similarly, but not exactly:

def snippet(thought)
  thought.split[0..9].join(" ") +
    (thought.split.size > 10 ? "..." : "")
end

cheers,
Mark