[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what's the Ruby way to do this?

Miles Keaton

1/5/2005 6:11:00 AM

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


15 Answers

Erik Veenstra

1/5/2005 6:20:00 AM

0

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.

Florian Gross

1/5/2005 7:16:00 AM

0

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 "..."?

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

Miles Keaton

1/5/2005 3:16:00 PM

0

Beautiful. Thanks guys!


William James

1/6/2005 11:30:00 AM

0

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

puts snippet("The quick red fox jumps over the lazy brown fat hog.")
puts snippet("The quick red fox jumps over the lazy brown hog.")
puts snippet("The quick red fox jumps over the lazy brown")

The quick red fox jumps over the lazy brown fat ...
The quick red fox jumps over the lazy brown hog.
The quick red fox jumps over the lazy brown

georgesawyer

1/7/2005 6:26:00 PM

0

>>match(/([^\s]+(\s+|$)){0,10}/)[0]+($'>""?"...":"")

This is a good way, seriously, for a Perl programmer moving to Ruby to
maintain job security.

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

Belorion

1/7/2005 6:42:00 PM

0

> 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

is more the "Ruby Way"


Douglas Livingstone

1/7/2005 7:12:00 PM

0

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

Shorter too.


William James

1/7/2005 8:02:00 PM

0

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.

Belorion

1/7/2005 9:43:00 PM

0

>> In which case I would support the notion that

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

>> 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.

If you take the sentence in full context, you will see that I am
making no such claim.

The full sentence read "In which case I would support the notion that
[...] is more the 'Ruby Way'"

"support the notion" is hardly proclaming myself the arbiter of what is proper.


Belorion

1/7/2005 9:52:00 PM

0

Sorry to post again so soon after my last post -- my reply box lost
focus and enter sent the message instead of a newline...

Anyway...


> 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

I did not mean to imply regular expressions are in poor form. They
are quite powerful, and I think Ruby would be crippled without them.
Rather, I was of the opinion that the other solutions were more
approriate for the simplicity of the problem, and that *always* using
regular expressions can complicate matters that don't need
complicating. One of the things I love about Ruby the most is the
simplicity of the solutions you can come up with.