Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Efficient way to do a simple Perl thing with Ruby

Reply
Thread Tools

Efficient way to do a simple Perl thing with Ruby

 
 
Kirk Haines
Guest
Posts: n/a
 
      07-08-2004
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



 
Reply With Quote
 
 
 
 
Kirk Haines
Guest
Posts: n/a
 
      07-08-2004
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



 
Reply With Quote
 
 
 
 
Randy Lawrence
Guest
Posts: n/a
 
      07-09-2004
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.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-09-2004

"Randy Lawrence" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fqlHc.1176$%(E-Mail Removed). ..
> 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

 
Reply With Quote
 
Graham Nicholls
Guest
Posts: n/a
 
      07-09-2004
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: What is the right way to do this simple thing Tim Rentsch C Programming 3 03-19-2012 08:33 PM
Re: What is the right way to do this simple thing ImpalerCore C Programming 9 03-13-2012 09:23 PM
implementing a simple and efficient index system Janus Bor Ruby 17 07-09-2008 08:00 AM
Cheap (simple, efficient) solution for literal strings in templates? Alf P. Steinbach C++ 4 03-24-2005 02:18 PM
one thing solved, but other terrible thing occur... Zam ASP General 1 03-14-2005 06:09 PM



Advertisments