Feature #6811

File, Dir and FileUtils should have bang-versions of singleton methods that fails silently

Added by Ilya Vorontsov over 1 year ago. Updated over 1 year ago.

[ruby-core:46854]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor

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

History

#1 Updated by Thomas Sawyer over 1 year 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?).

#2 Updated by Benoit Daloze over 1 year 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?

#3 Updated by Thomas Sawyer over 1 year ago

=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

#4 Updated by Benoit Daloze over 1 year 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')
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.

#5 Updated by Boris Stitnicky over 1 year 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.

#6 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto
  • Target version set to next minor

Also available in: Atom PDF