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

Use a block with file.new to auto-close file ref #191

Merged
merged 2 commits into from
Nov 4, 2015

Conversation

gschneider-r7
Copy link
Contributor

Fixes #190

I haven't tested this yet, but the behavior should be exactly the same except that the file will auto-close when the block ends. The export scan method also does this, although using the one-liner bracket syntax.

scan = ::File.new(zip_file, 'rb')
data.add_part(scan.read, 'application/zip', 'binary',
"form-data; name=\"scan\"; filename=\"#{zip_file}\"")
::File.new(zip_file, 'rb') do |scan|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gschneider-r7 Why do you need :: for ::File.new instead of directly calling File.new?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beats me, it was already there. I assumed there were good reasons at some point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so in this case, to my knowledge usually we use :: to call Module::Class to know which class we are calling. Could you please remove the ::?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remembered File.new doesn't accept a block, we need to use File.open instead.

(irb):2: warning: File::new() does not take block; use File::open() instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so in this case, to my knowledge usually we use :: to call Module::Class to know which class we are calling. Could you please remove the ::?

Using ::File over File I assume at one point (if not still the case) we had a conflicting constant in the Nexpose module namespace. Using ::File explicitly gets the top level constant (out of the Nexpose namespace) and leaving it alone doesn't affect this fix (while changing it could introduce a bug theoretically).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I thought File.new and File.open were actually the same method (aliases). I'll change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rkhalil-r7 @gschneider-r7 yep, definitely have at least one today see lib/nexpose/asset.rb.

@erran-r7
Copy link
Contributor

@gschneider-r7 Let's change to using File.open and :shipit:

gschneider-r7 added a commit that referenced this pull request Nov 4, 2015
Use a block with file.new to auto-close file ref
@gschneider-r7 gschneider-r7 merged commit 9085a7c into master Nov 4, 2015
@gschneider-r7 gschneider-r7 deleted the auto-close-import-file branch November 4, 2015 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import_scan function leaves behind open file handles
3 participants