Skip to content
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

Merged

Conversation

dylandrop
Copy link
Contributor

@dylandrop dylandrop commented Dec 18, 2016

Adds empty? to the File and Dir 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.

begin
stat(path).size == 0
rescue Errno
raise Errno.new("Error determining size of '#{path}'")
Copy link
Contributor Author

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)
Copy link
Member

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

Copy link
Contributor Author

@dylandrop dylandrop Dec 20, 2016

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.

@asterite
Copy link
Member

@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.

@dylandrop dylandrop force-pushed the add-file-and-dir-exists-methods branch from 9219d88 to a7683aa Compare December 20, 2016 15:10
@dylandrop
Copy link
Contributor Author

@asterite Done.

@asterite asterite added this to the 0.20.2 milestone Dec 20, 2016
@asterite
Copy link
Member

@dylandrop Thank you for this! 💚

@asterite asterite merged commit 0dcc030 into crystal-lang:master Dec 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants