Forum: Ruby-core [ruby-trunk - Feature #6811][Open] File, Dir and FileUtils should have bang-versions of singleton me

Posted by prijutme4ty (Ilya Vorontsov) (Guest)
on 2012-07-29 13:38
(Received via mailing list)
Issue #6811 has been reported by prijutme4ty (Ilya Vorontsov).

----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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
Posted by Thomas Sawyer (7rans)
on 2012-07-29 15:33
(Received via mailing list)
Issue #6811 has been updated by trans (Thomas Sawyer).


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?).

----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811#change-28533

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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
Posted by Eregon (Benoit Daloze) (Guest)
on 2012-07-29 17:27
(Received via mailing list)
Issue #6811 has been updated by Eregon (Benoit Daloze).


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?
----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811#change-28537

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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
Posted by Thomas Sawyer (7rans)
on 2012-07-29 20:08
(Received via mailing list)
Issue #6811 has been updated by trans (Thomas Sawyer).


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.

----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811#change-28540

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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
Posted by Benoit Daloze (Guest)
on 2012-07-29 20:27
(Received via mailing list)
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.
Posted by boris_stitnicky (Boris Stitnicky) (Guest)
on 2012-11-19 22:42
(Received via mailing list)
Issue #6811 has been updated by boris_stitnicky (Boris Stitnicky).


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.
----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811#change-33128

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


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
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-24 03:19
(Received via mailing list)
Issue #6811 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)
Target version set to next minor


----------------------------------------
Feature #6811: File, Dir and FileUtils should have bang-versions of 
singleton methods that fails silently
https://bugs.ruby-lang.org/issues/6811#change-33737

Author: prijutme4ty (Ilya Vorontsov)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.