File.delete one-liner

D

David Stanford

Hi guys,

I'm looking for a single line that essentially deletes the lockfile
argument passed to the unlock method, or deletes the instance variable
(default) lockfile. Like this, but simpler:

@lockfile = some_other_lockfile

def self.unlock(lockfile)
if File.exist?(lockfile)
File.delete(lockfile)
else
File.delete(@lockfile)
end
end


I was thinking of something along the following:

@lockfile = some_other_lockfile

def self.unlock
File.delete(lockfile) || File.delete(@lockfile)
end

...however, the File.delete method doesn't return a true/false value
based on its success.

Any tips? Thanks in advance!

-David
 
J

Joel VanderWerf

David said:
Hi guys,

I'm looking for a single line that essentially deletes the lockfile
argument passed to the unlock method, or deletes the instance variable
(default) lockfile. Like this, but simpler:

@lockfile = some_other_lockfile

def self.unlock(lockfile)
if File.exist?(lockfile)
File.delete(lockfile)
else
File.delete(@lockfile)
end
end


I was thinking of something along the following:

@lockfile = some_other_lockfile

def self.unlock
File.delete(lockfile) || File.delete(@lockfile)
end

...however, the File.delete method doesn't return a true/false value
based on its success.

Any tips? Thanks in advance!

-David

Use exceptions?

begin
File.delete(lf)
rescue Errno::ENOENT
File.delete(@lf)
end
 
D

David Stanford

Use exceptions?
begin
File.delete(lf)
rescue Errno::ENOENT
File.delete(@lf)
end

Thanks Joel. Forgive me, I'm just learning Ruby/scripting/programming. I
believe I understand your example, but (as far as I can tell) it doesn't
really accomplish my goal, and seems to have the same functionality of
my first (longer) example.

Please let me know if I've missed something. :)

In any case, I think I found a (seemingly) obvious solution to my
question:

@lockfile = some_other_lockfile

def self.unlock(lockfile)
File.exist?(lockfile) ? File.delete(lockfile) : File.delete(@lockfile)
end


Thanks again!

-David
 
J

Joel VanderWerf

David said:
Thanks Joel. Forgive me, I'm just learning Ruby/scripting/programming. I
believe I understand your example, but (as far as I can tell) it doesn't
really accomplish my goal, and seems to have the same functionality of
my first (longer) example.

Please let me know if I've missed something. :)

In any case, I think I found a (seemingly) obvious solution to my
question:

@lockfile = some_other_lockfile

def self.unlock(lockfile)
File.exist?(lockfile) ? File.delete(lockfile) : File.delete(@lockfile)
end

With exceptions, you are saying "try to delete lf; if that fails
(because the file doesn't exist) then try to delete @lf". Other failures
(e.g., lf permissions are wrong, or @lf doesn't exist) are reported
normally as exceptions.

One advantage to doing it this way is that the delete is an atomic
operation. With

File.exist?(lockfile) ? File.delete(lockfile) : File.delete(@lockfile)

there is the possiblity that #exist? will return true, but in the brief
time before #delete is called, some other process deletes the file.

Btw, it looks to me like your #unlock implementation above is the same
as your first implementation. The ? : construct is really the same as
if...else, just more compact.
 
R

Robert Klemme

2009/2/4 Julian Leviston said:
File.delete(File.exists?(lockfile) ? lockfile : @lockfile)

[lockfile, @lockfile].any? {|f| File.delete(f) rescue false}

Cheers

robert
 
J

Joel VanderWerf

Robert said:
2009/2/4 Julian Leviston said:
File.delete(File.exists?(lockfile) ? lockfile : @lockfile)

[lockfile, @lockfile].any? {|f| File.delete(f) rescue false}

If #delete fails on lockfile for some reason other than ENOENT, this
code will try to delete @lockfile.
 
R

Robert Klemme

Robert said:
2009/2/4 Julian Leviston said:
File.delete(File.exists?(lockfile) ? lockfile : @lockfile)
[lockfile, @lockfile].any? {|f| File.delete(f) rescue false}

If #delete fails on lockfile for some reason other than ENOENT, this
code will try to delete @lockfile.

This is correct. I thought that was intended. Didn't you suggest
exactly this in your previous posting (atomic deletion)? Looking at the
original question it is not clear in which cases @lockfile should be
deleted - unless you take the code as spec.

Thanks for pointing this out!

Cheers

robert
 
J

Joel VanderWerf

Robert said:
Robert said:
2009/2/4 Julian Leviston <[email protected]>:
File.delete(File.exists?(lockfile) ? lockfile : @lockfile)
[lockfile, @lockfile].any? {|f| File.delete(f) rescue false}

If #delete fails on lockfile for some reason other than ENOENT, this
code will try to delete @lockfile.

This is correct. I thought that was intended. Didn't you suggest
exactly this in your previous posting (atomic deletion)? Looking at the
original question it is not clear in which cases @lockfile should be
deleted - unless you take the code as spec.

Thanks for pointing this out!

The only difference in my suggestion was to "Rescue Errno::ENOENT",
which seems closer to the original intent of using #exists?...

begin
File.delete(lf)
rescue Errno::ENOENT
File.delete(@lf)
end
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top