Feature #6811
openFile, Dir and FileUtils should have bang-versions of singleton methods that fails silently
Description
I found that often write
Dir.mkdir(folder) unless Dir.exist? folder
and similar code for rm, rm_rf and so on
We can simply make bang-versions like
def Dir.mkdir!(folder, permissions=0744)
Dir.mkdir(folder, permissions) unless Dir.exist? folder
or another alternative Dir.mkdir(folder, permissions) rescue false¶
end
Updated by trans (Thomas Sawyer) over 12 years ago
At least for mkdir you can use mkdir_p, although it annoyingly warns about missing path if $VERBOSE=true (can we consider that a bug please?).
Updated by Eregon (Benoit Daloze) over 12 years ago
prijutme4ty (Ilya Vorontsov) wrote:
I found that often write
Dir.mkdir(folder) unless Dir.exist? folder
and similar code for rm, rm_rf and so on
Bang methods are not usually used for that in Ruby core and stdlib.
I agree for the practical point of view of this though.
I'm unsure what a good name would be.
Using mkdir_p for this seems a bit dangerous, as it might create intermediary directories you did not intend to.
trans wrote:
although it annoyingly warns about missing path if $VERBOSE=true (can we consider that a bug please?).
How so?
Updated by trans (Thomas Sawyer) over 12 years ago
=begin
Sorry, it is if $DEBUG = true, not $VERBOSE.
Here's the issue:
FileUtils.mkdir_p('a/b/c/d')
ExceptionErrno::ENOENT' at /home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/fileutils.rb:247 - No such file or directory - a/b/c/d Exception
Errno::EEXIST' at /home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/fileutils.rb:247 - File exists - .
=> ["a/b/c/d"]
It the most annoying warning, and I always end-up writing my own method instead.
=end
Updated by Eregon (Benoit Daloze) over 12 years ago
On 29 July 2012 20:07, trans (Thomas Sawyer) transfire@gmail.com wrote:
Sorry, it is if $DEBUG = true, not $VERBOSE.
Here's the issue:
FileUtils.mkdir_p('a/b/c/d')
ExceptionErrno::ENOENT' at /home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/fileutils.rb:247 - No such file or directory - a/b/c/d Exception
Errno::EEXIST' at /home/trans/.local/lib/ry/rubies/1.9.3-p125/lib/ruby/1.9.1/fileutils.rb:247 - File exists - .
=> ["a/b/c/d"]It the most annoying warning, and I always end-up writing my own method instead.
Ah, I see, this is due to the exception-driven implementation of FileUtils.
I think it would be worth discussing it in a separate issue.
Updated by Anonymous about 12 years ago
First, let me apologize to everyone for expressing opinion, while having relatively little experience. I like your proposal, but I am concerned about feature creep. Maybe those convenience features should go to stdlib, just like eg. require 'mathn' changes math behavior, we could have require 'fileboost' or something making FileUtils fatter. I think that in the future, there will be many amazing proposals for FileUtils functionality, like yours here, so the best of them could go to this 'fileboost' or whatever other name would it have.
Updated by mame (Yusuke Endoh) almost 12 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
- Target version set to 2.6