[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

backtick vrs native ruby

Mark Probert

2/17/2005 8:09:00 PM


Hi ..

In one of my apps, I am doing something like

def clean
f = `rm -f #{@datadir}/*.raw`
end

I could, just as easily do

def clean
Dir["#{@datadir}/*.raw"].each do |f| File.delete(f) end
end

Besides the portability aspect (does 'rm' exist on the box?), is one to be
preferred to the other?

Regards,

--
-mark. (probertm at acm dot org)


2 Answers

Andre Nathan

2/17/2005 8:29:00 PM

0

Hi Mark

On Fri, 18 Feb 2005, Mark Probert wrote:

> Besides the portability aspect (does 'rm' exist on the box?), is one to be
> preferred to the other?

I'd go with

require 'fileutils'
FileUtils.rm_f Dir.glob("#{@datadir}/*.raw")


Andre


Stefan Schmiedl

2/18/2005 9:20:00 AM

0

On Fri, 18 Feb 2005 05:09:29 +0900,
Mark Probert <probertm@acm.org> wrote:
>
> Hi ..
>
> In one of my apps, I am doing something like
>
> def clean
> f = `rm -f #{@datadir}/*.raw`
> end
>
> I could, just as easily do
>
> def clean
> Dir["#{@datadir}/*.raw"].each do |f| File.delete(f) end
> end
>
> Besides the portability aspect (does 'rm' exist on the box?), is one to be
> preferred to the other?

I'd go with the second one for everything that's not a one-time hack.
Depending on where the contents of @datadir originate, you might have
a security problem in the first method.

And you're starting an external process, which might become expensive,
if done often enough.

kind regards,
s.