[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby vs Python vs Perl (programming example

Laura Raffa

3/1/2005 11:22:00 PM

Hi,

I have no experience with Ruby, but there is currently a debate in the
office about Python over Perl, Java, C++ etc. It's a simple programming
example:

find and print all words ending with some predefined suffix

An array of words can be assumed. The shortest possible code solution (and
also the most elegant) is required. A Perl solution with the -n option was
disallowed.

As I don't know Ruby, can anyone give me a quick example. The Ruby guy who
rabbits on about the excellence of Ruby isn't here to day. Thought I'd
check.


Thanks


12 Answers

Assaph Mehr

3/1/2005 11:55:00 PM

0

> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution
(and
> also the most elegant) is required. A Perl solution with the -n
option was
> disallowed.

Assume:
.. words = [...] # array
.. suffix = "..." # string

Then:
.. puts words.select { |word| word =~ /#{suffix}$/ }

Dave Burt

3/2/2005 2:11:00 AM

0


"Laura Raffa" <lraffa@ozemail.com.au> wrote:
> Hi,
>
> I have no experience with Ruby, but there is currently a debate in the
> office about Python over Perl, Java, C++ etc. It's a simple programming
> example:
>
> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution (and
> also the most elegant) is required. A Perl solution with the -n option was
> disallowed.
>

Ruby:
puts words.select { |word| /#{suffix}$/ =~ word }
or
words.each {|word| puts word if /#{suffix}$/ =~ word }
or
for word in words
puts word if /#{suffix}$/ =~ word
end

Perl:
print grep /$suffix$/, @words;
or
for (@words) { print if /$suffix$/ }

I think I prefer Perl's solution for such a trivial example. You're only
going to see the superiority of Ruby over Perl in a bigger problem, so you
can crack out your elegant class definitions and reflection and general
dynamism, and watch the Perl hacker fight with references and {$%@#/&***/
Cheers,
Dave


Csaba Henk

3/2/2005 5:23:00 AM

0

On 2005-03-01, Assaph Mehr <assaph@gmail.com> wrote:
>> find and print all words ending with some predefined suffix
>>
>> An array of words can be assumed. The shortest possible code solution
> (and
>> also the most elegant) is required. A Perl solution with the -n
> option was
>> disallowed.
>
> Assume:
> . words = [...] # array
> . suffix = "..." # string
>
> Then:
> . puts words.select { |word| word =~ /#{suffix}$/ }

Maybe /#{Regexp.quote suffix}$/, if you want to be general...

Csaba

gabriele renzi

3/2/2005 6:14:00 AM

0

Assaph Mehr ha scritto:
>>find and print all words ending with some predefined suffix
>>
>>An array of words can be assumed. The shortest possible code solution
>
> (and
>
>>also the most elegant) is required. A Perl solution with the -n
>
> option was
>
>>disallowed.
>
>
> Assume:
> . words = [...] # array
> . suffix = "..." # string
>
> Then:
> . puts words.select { |word| word =~ /#{suffix}$/ }

for String there is a special method of Enumerable, since this is an
important case:
puts words.grep(/suffix$/)

Dave Burt

3/2/2005 6:34:00 AM

0


"gabriele renzi" <surrender_it@remove-yahoo.it> ha scritto:
> Assaph Mehr ha scritto:
>> . puts words.select { |word| word =~ /#{suffix}$/ }
>
> for String there is a special method of Enumerable, since this is an
> important case:
> puts words.grep(/suffix$/)

Cool! It's not a special case, it's a case-equality match:

-------------------------------------------------------- Enumerable#grep
enum.grep(pattern) => array
enum.grep(pattern) {| obj | block } => array
------------------------------------------------------------------------
Returns an array of every element in _enum_ for which +Pattern ===
element+. If the optional _block_ is supplied, each matching
element is passed to it, and the block's result is stored in the
output array.

a = [nil, 1, 3.5, 'a', nil, 10**100, [], {}, 'b', 'c']
a.grep(NilClass).size #=> 2
a.grep(String) #=> ['a', 'b', 'c']
a.grep(Numeric) #=> [1, 3.5, 10000...]

So you can do regex-matches on string arrays, and kind_of?-matches, and
equality-matches.

Cheers,
Dave


pit

3/2/2005 8:32:00 AM

0

"Laura Raffa" <lraffa@ozemail.com.au> wrote in message news:<AE6Vd.11$hX4.794@nnrp1.ozemail.com.au>...
>
> find and print all words ending with some predefined suffix

puts words.grep(/#{suffix}$/)

Regards,
Pit

gabriele renzi

3/2/2005 9:13:00 AM

0

Dave Burt ha scritto:
> "gabriele renzi" <surrender_it@remove-yahoo.it> ha scritto:
>
>>Assaph Mehr ha scritto:
>>
>>>. puts words.select { |word| word =~ /#{suffix}$/ }
>>
>>for String there is a special method of Enumerable, since this is an
>>important case:
>> puts words.grep(/suffix$/)
>
>
> Cool! It's not a special case, it's a case-equality match:

Well, I think that is the generalization of the special case :D
as the name suggest, I think it was initially done for finding strings

Guyver2

3/2/2005 12:37:00 PM

0

"Laura Raffa" <lraffa@ozemail.com.au> wrote in message news:<AE6Vd.11$hX4.794@nnrp1.ozemail.com.au>...
> Hi,
>
> I have no experience with Ruby, but there is currently a debate in the
> office about Python over Perl, Java, C++ etc. It's a simple programming
> example:
>
> find and print all words ending with some predefined suffix
>
> An array of words can be assumed. The shortest possible code solution (and
> also the most elegant) is required. A Perl solution with the -n option was
> disallowed.
>
> As I don't know Ruby, can anyone give me a quick example. The Ruby guy who
> rabbits on about the excellence of Ruby isn't here to day. Thought I'd
> check.
>
>
> Thanks

http://www.approximity.com/ruby/Comparison_rb_st_m...
http://www.angelfire.com/tx4/c...

Kristof Bastiaensen

3/2/2005 1:23:00 PM

0

On Wed, 02 Mar 2005 00:32:05 -0800, Pit Capitain wrote:

> "Laura Raffa" <lraffa@ozemail.com.au> wrote in message news:<AE6Vd.11$hX4.794@nnrp1.ozemail.com.au>...
>>
>> find and print all words ending with some predefined suffix
>
> puts words.grep(/#{suffix}$/)
>
> Regards,
> Pit

Nice. If suffix contains regexp characters it is safer to quote them:

puts words.grep(/#{Regexp.quote(suffix)}$/)

Regards,
KB


Gene Tani

3/3/2005 6:52:00 AM

0

http://www.ruby-doc.org/RubyEyeForThePyth...

and the appendices "Perl vs. Ruby" and "Python vs. Ruby" in the back of
Hal Fulton "Ruby Way" book.