Skip to content

Commit b77446c

Browse files
committed
Add a rule about auto resource releasing methods
1 parent 2d7e03d commit b77446c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,23 @@ condition](#safe-assignment-in-condition).
27592759
end
27602760
```
27612761

2762+
* <a name="auto-release-resources"></a>
2763+
Use versions of resource obtaining methods that do automatic
2764+
resource cleanup when possible.
2765+
<sup>[[link](#auto-release-resources)]</sup>
2766+
2767+
```Ruby
2768+
# bad - you need to close the file descriptor explicitly
2769+
f = File.open('testfile')
2770+
# ...
2771+
f.close
2772+
2773+
# good - the file descriptor is closed automatically
2774+
File.open('testfile') do |f|
2775+
# ...
2776+
end
2777+
```
2778+
27622779
* <a name="standard-exceptions"></a>
27632780
Favor the use of exceptions for the standard library over introducing new
27642781
exception classes.

0 commit comments

Comments
 (0)