The upcoming Ruby 2.4 brings Dir.empty?
and File.empty?
methods for finding out if a dir or a file is empty. See https://github.com/ruby/ruby/blob/trunk/NEWS
I think these methods are quite useful for some use cases and they could be handy as being a part of standard library.
Sounds useful.
I checked the Ruby docs, I'm not sure I agree with the behaviour:
File.empty?
returns true if the file exists and it's emptyDir.empty?
returns true if the dir doesn't exist, or if it exists and it's emptyShouldn't Dir.empty?
return false if the dir doesn't exist? I don't know why the made it work like that.
Unfortunately no real discussion about that in https://bugs.ruby-lang.org/issues/10121 either.
If Dir.empty?
for nonexistent dir returned false, then it meant that it does contain things? but it doesn't exist, so it can't contain anything, can it?
@ysbaddaden Agreed. But why does File.empty?
returns false
for a nonexistent file then? If it's false it means it's not empty, so it surely contains something, right?
No clue :-/
@asterite Because you can't say the file is empty (_true_ value) when it doesn't exit. The opposite of it is _false_ value. For me it makes sense.
@deepj Then why Dir.empty?
returns true
when the directory doesn't exist?
@asterite In this case that doesn't make sense to me.
in ruby File.open("filename") raises Errno::ENOENT
Perhaps Dir.empty? and File.empty? should also raise if it doesn't exists?
If someone asks you if you can check the beer bottle is empty and upon checking you realise the beer bottle is not there, you go back and say: sorry I don't know. You can't assume it's empty and let alone be certain about it's contents. Perhaps the file or dir are also located in a different place you were expecting it to be.
@benoist agree. Same thing in case of permission problems etc.
I agree with @benoist
The use of Dir.empty?
and File.empty?
should be straightforward, and raising errors allows those methods to be so :)
Anyone on it btw ?
@terry90 See #3724.
Most helpful comment
in ruby File.open("filename") raises Errno::ENOENT
Perhaps Dir.empty? and File.empty? should also raise if it doesn't exists?
If someone asks you if you can check the beer bottle is empty and upon checking you realise the beer bottle is not there, you go back and say: sorry I don't know. You can't assume it's empty and let alone be certain about it's contents. Perhaps the file or dir are also located in a different place you were expecting it to be.