backtick vrs native ruby

M

Mark Probert

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,
 
A

Andre Nathan

Hi Mark

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
 
S

Stefan Schmiedl

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top