[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

File.delete one-liner

David Stanford

2/3/2009 6:00:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

Joel VanderWerf

2/3/2009 6:08:00 PM

0

David Stanford wrote:
> 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


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

David Stanford

2/3/2009 8:29:00 PM

0

> 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
--
Posted via http://www.ruby-....

Joel VanderWerf

2/3/2009 9:35:00 PM

0

David Stanford wrote:
>> 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

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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Julian Leviston

2/4/2009 2:06:00 AM

0

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

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

On 04/02/2009, at 5:07 AM, Joel VanderWerf <vjoel@path.berkeley.edu>
wrote:

> David Stanford wrote:
>> 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
>
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>

Robert Klemme

2/5/2009 10:01:00 AM

0

2009/2/4 Julian Leviston <julian@coretech.net.au>:
> File.delete(File.exists?(lockfile) ? lockfile : @lockfile)

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

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Joel VanderWerf

2/5/2009 5:49:00 PM

0

Robert Klemme wrote:
> 2009/2/4 Julian Leviston <julian@coretech.net.au>:
>> 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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Klemme

2/5/2009 8:05:00 PM

0

On 05.02.2009 18:49, Joel VanderWerf wrote:
> Robert Klemme wrote:
>> 2009/2/4 Julian Leviston <julian@coretech.net.au>:
>>> 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

Joel VanderWerf

2/5/2009 10:06:00 PM

0

Robert Klemme wrote:
> On 05.02.2009 18:49, Joel VanderWerf wrote:
>> Robert Klemme wrote:
>>> 2009/2/4 Julian Leviston <julian@coretech.net.au>:
>>>> 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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407