Ruby vs Python vs Perl (programming example)

Discussion in 'Ruby' started by Laura Raffa, Mar 1, 2005.

  1. Laura Raffa

    Laura Raffa Guest

    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
    Laura Raffa, Mar 1, 2005
    #1
    1. Advertising

  2. Laura Raffa

    Assaph Mehr Guest

    > 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}$/ }
    Assaph Mehr, Mar 1, 2005
    #2
    1. Advertising

  3. Laura Raffa

    Dave Burt Guest

    "Laura Raffa" <> 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
    Dave Burt, Mar 2, 2005
    #3
  4. Laura Raffa

    Csaba Henk Guest

    On 2005-03-01, Assaph Mehr <> 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
    Csaba Henk, Mar 2, 2005
    #4
  5. 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$/)
    gabriele renzi, Mar 2, 2005
    #5
  6. Laura Raffa

    Dave Burt Guest

    "gabriele renzi" <> 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
    Dave Burt, Mar 2, 2005
    #6
  7. Laura Raffa

    Pit Capitain Guest

    "Laura Raffa" <> wrote in message news:<AE6Vd.11$>...
    >
    > find and print all words ending with some predefined suffix


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

    Regards,
    Pit
    Pit Capitain, Mar 2, 2005
    #7
  8. Dave Burt ha scritto:
    > "gabriele renzi" <> 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
    gabriele renzi, Mar 2, 2005
    #8
  9. Laura Raffa

    Guyver2 Guest

    "Laura Raffa" <> wrote in message news:<AE6Vd.11$>...
    > 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/
    Guyver2, Mar 2, 2005
    #9
  10. On Wed, 02 Mar 2005 00:32:05 -0800, Pit Capitain wrote:

    > "Laura Raffa" <> wrote in message news:<AE6Vd.11$>...
    >>
    >> 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
    Kristof Bastiaensen, Mar 2, 2005
    #10
  11. Laura Raffa

    Guest

    , Mar 3, 2005
    #11
  12. Laura Raffa

    BearItAll Guest

    Laura Raffa 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.
    >
    > 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.
    BearItAll, Mar 3, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Monyatovsky

    Re: Python vs Perl (an example)

    Nick Monyatovsky, Apr 1, 2004, in forum: Python
    Replies:
    1
    Views:
    339
    Ville Vainio
    Apr 1, 2004
  2. Sam Roberts
    Replies:
    15
    Views:
    292
    Sam Roberts
    Feb 7, 2005
  3. John Maclean
    Replies:
    7
    Views:
    143
    Dave Thomas
    Jan 17, 2006
  4. Mike Glaz
    Replies:
    6
    Views:
    145
  5. Hiro Protagonist
    Replies:
    3
    Views:
    115
    Hiro Protagonist
    Oct 29, 2008
Loading...

Share This Page