-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add File and Dir empty? methods #3724
Add File and Dir empty? methods #3724
Conversation
begin | ||
stat(path).size == 0 | ||
rescue Errno | ||
raise Errno.new("Error determining size of '#{path}'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This captures and re-raises the error, with a different message. I don't know if there's a more elegant / obvious way to do this.
raise Errno.new("Error determining size of '#{path}'") unless exists?(path) | ||
|
||
foreach(path) do |f| | ||
return false unless %(. ..).includes?(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this check is correct. %(. ..)
is the string ". ..". Maybe it's safer to do {".", ".."}.includes?(f)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 You're right -- originally I meant to do %w
, but the tuple way is better. Updating.
I think it worked just by happenstance of the way the String#includes?
works.
@dylandrop I believe this is the correct way to implement these methods, the semantic you mention is pretty good :-) I just made a tiny comment, after that I'll merge this. |
9219d88
to
a7683aa
Compare
@asterite Done. |
@dylandrop Thank you for this! 💚 |
Adds
empty?
to theFile
andDir
classes. Closes #2951.This follows the proposed behavior of this comment. This varies from Ruby in that an error is raised if the file / dir is not found.
In terms of implementation -- the File version is pretty much identical to Ruby's. Interestingly, their implementation for Dir seems to have an optimization that leverages getattrlist. I didn't know if this was super valuable, but it's an optimization that could be added in a later PR.