Ruby vs Python vs Perl (programming example)

L

Laura Raffa

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
 
A

Assaph Mehr

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}$/ }
 
D

Dave Burt

Laura Raffa said:
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
 
C

Csaba Henk

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
 
G

gabriele renzi

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$/)
 
D

Dave Burt

gabriele renzi said:
Assaph Mehr ha scritto:

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
 
G

gabriele renzi

Dave Burt ha scritto:
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
 
G

Guyver2

Laura Raffa said:
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_java.html
http://www.angelfire.com/tx4/cus/shapes/
 
K

Kristof Bastiaensen

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
 
B

BearItAll

Laura said:
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

I don't see why perl should wear a handycap.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top