R
Ronald Fischer
In my application, I often have blocks of code, where during preparation
code, I need to make sure that certain files do not exist (in practice,
this might be files left over from a previous run, which I didn't want
to have erased earlier). Basically, I am doing something like this:
if File.exist?(filename)
File.unlink(filename)
end
or, equivalently,
begin
File.unlink(filename)
rescue=20
# ignore errors - it's OK if the file does not exist
end=20
This is necessary, because File::unlink signals non-existence of the
file
using an exception, instead by return code (as, for example, Perl's
unlink
does). The resulting code looks clumsy, because one always has to take
care
of an exception, which does not really signal an error condition (in
Perl,
I would simply ignore the return code of unlink).
Of course I could write my own unlink function like this:
def silent_unlink(f)
File.unlink(f) if File.exist?(f)
end
...
silent_unlink(filename)
but I am wondering whether this is not a such common problem, that Ruby=20
already might have a non-exception-throwing unlink function built in
somewhere?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
=20
code, I need to make sure that certain files do not exist (in practice,
this might be files left over from a previous run, which I didn't want
to have erased earlier). Basically, I am doing something like this:
if File.exist?(filename)
File.unlink(filename)
end
or, equivalently,
begin
File.unlink(filename)
rescue=20
# ignore errors - it's OK if the file does not exist
end=20
This is necessary, because File::unlink signals non-existence of the
file
using an exception, instead by return code (as, for example, Perl's
unlink
does). The resulting code looks clumsy, because one always has to take
care
of an exception, which does not really signal an error condition (in
Perl,
I would simply ignore the return code of unlink).
Of course I could write my own unlink function like this:
def silent_unlink(f)
File.unlink(f) if File.exist?(f)
end
...
silent_unlink(filename)
but I am wondering whether this is not a such common problem, that Ruby=20
already might have a non-exception-throwing unlink function built in
somewhere?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
=20