Efficient way to do a simple Perl thing with Ruby

Discussion in 'Ruby' started by Kirk Haines, Jul 8, 2004.

  1. Kirk Haines

    Kirk Haines Guest

    I have some Perl code that does something like this:

    $copy =~ s{<!--#include\s+virtual\s*=\s*"([^"]*)"\s*-->}{get($1)}gem

    Basically, apply a regex to a string, and for every match, call get($1) and
    replace the match with the return value of the get(). get() comes from
    LWP::Simple, and simply fetches the content at the given URL.

    My Ruby-fu is failing me at coming up with a relatively concise yet readable
    way to do this in Ruby. My implementation is, I am sure, much longer than
    necessary.

    So, anyone have any ideas? How would you do this?


    Kirk Haines
    Kirk Haines, Jul 8, 2004
    #1
    1. Advertising

  2. Kirk Haines

    Kirk Haines Guest

    On Fri, 9 Jul 2004 01:21:29 +0900, Kirk Haines wrote

    > My Ruby-fu is failing me at coming up with a relatively concise yet
    > readable way to do this in Ruby. My implementation is, I am sure,
    > much longer than necessary.
    >
    > So, anyone have any ideas? How would you do this?


    And....nevermind. I figured out where I was being thick. gsub with a block
    works nicely.


    Kirk Haines
    Kirk Haines, Jul 8, 2004
    #2
    1. Advertising

  3. Kirk Haines wrote:
    > On Fri, 9 Jul 2004 01:21:29 +0900, Kirk Haines wrote
    >
    >
    >>My Ruby-fu is failing me at coming up with a relatively concise yet
    >>readable way to do this in Ruby. My implementation is, I am sure,
    >>much longer than necessary.
    >>
    >>So, anyone have any ideas? How would you do this?

    >
    >
    > And....nevermind. I figured out where I was being thick. gsub with a block
    > works nicely.
    >
    >
    > Kirk Haines
    >
    >
    >


    We ran into the same question earlier today. Maybe the documentation
    for String.sub() should mention String.gsub(). I think it would have
    been more intuitive to have an optional parameter to sub() for
    global/case-insenstive/etc.
    Randy Lawrence, Jul 9, 2004
    #3
  4. "Randy Lawrence" <> schrieb im Newsbeitrag
    news:fqlHc.1176$%...
    > Kirk Haines wrote:
    > > On Fri, 9 Jul 2004 01:21:29 +0900, Kirk Haines wrote
    > >
    > >
    > >>My Ruby-fu is failing me at coming up with a relatively concise yet
    > >>readable way to do this in Ruby. My implementation is, I am sure,
    > >>much longer than necessary.
    > >>
    > >>So, anyone have any ideas? How would you do this?

    > >
    > >
    > > And....nevermind. I figured out where I was being thick. gsub with a

    block
    > > works nicely.
    > >
    > >
    > > Kirk Haines
    > >
    > >
    > >

    >
    > We ran into the same question earlier today. Maybe the documentation
    > for String.sub() should mention String.gsub().


    Good point.

    > I think it would have
    > been more intuitive to have an optional parameter to sub() for
    > global/case-insenstive/etc.


    Case sensitivity is controlled by a regular expression flag.

    About gsub vs. sub with a paramter that controls global replacement: while
    that might mimic what vi does, it's generally considered better style to
    have separate methods if behavior differs than to have a single method
    whose behavior is controlled by flags. Also it's more efficient, too.

    Kind regards

    robert
    Robert Klemme, Jul 9, 2004
    #4
  5. Randy Lawrence wrote:

    >
    > We ran into the same question earlier today. Maybe the documentation
    > for String.sub() should mention String.gsub().

    Funny, I wrote "see gsub for global occurrences" in "Programming Ruby's"
    chapter 22 about half an hour ago. It ought to have been there already,
    IMNSHO.
    Graham

    --
    With Linux, the answer's always "Yes"
    Graham Nicholls, Jul 9, 2004
    #5
    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. Rafa³ \ra_v\ Zawierta

    GMP problem with simple thing

    Rafa³ \ra_v\ Zawierta, Dec 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    297
    Kevin Goodsell
    Dec 2, 2003
  2. Zam
    Replies:
    1
    Views:
    217
    Mark Schupp
    Mar 14, 2005
  3. David Garamond
    Replies:
    21
    Views:
    252
    Gergely Kontra
    Jun 1, 2004
  4. ImpalerCore

    Re: What is the right way to do this simple thing

    ImpalerCore, Mar 10, 2012, in forum: C Programming
    Replies:
    9
    Views:
    237
    Ian Collins
    Mar 13, 2012
  5. Tim Rentsch

    Re: What is the right way to do this simple thing

    Tim Rentsch, Mar 10, 2012, in forum: C Programming
    Replies:
    3
    Views:
    250
    Tim Rentsch
    Mar 19, 2012
Loading...

Share This Page