Feature #6811
open
File, Dir and FileUtils should have bang-versions of singleton methods that fails silently
Added by prijutme4ty (Ilya Vorontsov) over 12 years ago.
Updated almost 7 years ago.
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
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?).
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?
=begin
Sorry, it is if $DEBUG = true, not $VERBOSE.
Here's the issue:
FileUtils.mkdir_p('a/b/c/d')
Exception Errno::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
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')
Exception Errno::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.
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.
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
- Target version set to 2.6
- Target version deleted (
2.6)
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0