georgesawyer wrote:
> >>match(/([^\s]+(\s+|$)){0,10}/)[0]+($'>""?"...":"")
>
> This is a good way, seriously, for a Perl programmer moving to Ruby
to
> maintain job security.
>

I don't know Perl.  However, any programmer worth his salt
knows regular expressions.  I think you would be well rewarded
if you looked into them.

> Ruby is using the tactic, "embrace and extend."

Then why haven't you embraced regular expressions and the
"Perl way"?


Belorion  wrote
>
> > def snippet(thought)
> > thought.match(/([^\s]+(\s+|$)){0,10}/)[0]+($'>""?"...":"")
> > end
>
>
> That is most certainly a precise and accurate solution, but to me the
> "Ruby Way" is short, conscise code which is *readable*
>
> In which case I would suppor the notion that
>
> > def snippet(thought)
> >  thought.split[0..9].join(" ") + "..."
> > end


The programmers among you will have seen that my solution wasn't
designed to be equivalent to the above code; instead it emulates
Gross's solution:

def snippet(thought)
words = thought.split
result = words.first(10).join(" ")
result += "..." if words.size > 10
return result
end

"..." is appended *only* if there are remaining words.


Douglas Livingstone wrote
>> > def snippet(thought)
>> >  thought.split[0..9].join(" ") + "..."
>> > end
>
>> is more the "Ruby Way"
>
>Shorter too.

Egad!  Another one!


Belorion  wrote
>
> is more the "Ruby Way"
>

I didn't realize that you are the arbiter of what is proper
use of Ruby.  Glad to meet you, Belorian.

A fellow that doesn't have your high authority wrote something
with which I concur:

"The common term for patterns that use this strange vocabulary
is regular expressions. In ruby, as in Perl, they are generally
surrounded by forward slashes rather than double quotes. If you
have never worked with regular expressions before, they
probably look anything but regular, but you would be wise
to spend some time getting familiar with them. They have an
efficient expressive power that will save you headaches (and
many lines of code) whenever you need to do pattern matching,
searching, or other manipulations on text strings."

Admittedly, his words don't carry as much weight as yours,
but I think they make a lot of sense.  Note where he says that
regular expressions can replace "many lines of code", a practice
of which he seems to approve.  Please forgive him, if not me,
Belorian.

"If you have never worked with regular expressions before,
they probably look anything but regular".

How true that is!  I am not as familiar with Ruby-style
regular expressions as I would like to be, since I've done
much more programming in Awk, which has less powerful ones.
I see now that I could have made it shorter (and I wouldn't
be surprised if Florian Gross could make it "yet shorter"):

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

It's really quite straightforward.  Broken down:

thought.match(
/         Begin reg.exp.
(         Begin group.
\S+       Match non-whitespace sequence.
(\s+|$)   Match whitespace or end of string.
)         End group.
{0,10}    Match group up to 10 times (grab 10 words).
/         End reg.exp.
)[0]      The matched portion of string.
+         Concatenate 2 strings.
($'>""    If the rest of the string following the match isn't empty...
?"...":"")    ... tack on "..."; else tack on empty string.