Forum: Ruby File.delete one-liner

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C96b9e285e706bfb3c4cac218ca9b4f4?d=identicon&s=25 David Stanford (dthomas53)
on 2009-02-03 18:57
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-03 19:08
(Received via mailing list)
David Stanford wrote:
>     File.delete(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
C96b9e285e706bfb3c4cac218ca9b4f4?d=identicon&s=25 David Stanford (dthomas53)
on 2009-02-03 21:29
> 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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-03 22:35
(Received via mailing list)
David Stanford wrote:
> really accomplish my goal, and seems to have the same functionality of
>   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.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-04 03:06
(Received via mailing list)
File.delete(File.exists?(lockfile) ? lockfile : @lockfile)

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

On 04/02/2009, at 5:07 AM, Joel VanderWerf <vjoel@path.berkeley.edu>
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-05 11:01
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-05 18:50
(Received via mailing list)
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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-05 21:10
(Received via mailing list)
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-02-05 23:07
(Received via mailing list)
Robert Klemme wrote:
> 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
This topic is locked and can not be replied to.