diff options
| author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-01-20 12:27:53 +0000 |
|---|---|---|
| committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-01-20 12:27:53 +0000 |
| commit | cdb561282ab2e21854bad602331bfd000af91c14 (patch) | |
| tree | 74920bcfcfb55c91c4be4d48b00e702a5c65aaf3 | |
| parent | 6027eb1ed511b818023d7759737ddb6972e923ac (diff) | |
| download | ruby-cdb561282ab2e21854bad602331bfd000af91c14.tar.gz ruby-cdb561282ab2e21854bad602331bfd000af91c14.tar.xz ruby-cdb561282ab2e21854bad602331bfd000af91c14.zip | |
* lib/tempfile.rb (self.open): If a block is given, call it with
tempfile as an argument and automatically close the tempfile
when the block terminates.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@3376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | lib/tempfile.rb | 20 |
2 files changed, 24 insertions, 2 deletions
@@ -1,3 +1,9 @@ +Mon Jan 20 21:25:18 2003 Akinori MUSHA <knu@iDaemons.org> + + * lib/tempfile.rb (self.open): If a block is given, call it with + tempfile as an argument and automatically close the tempfile + when the block terminates. + Mon Jan 20 21:02:50 2003 Akinori MUSHA <knu@iDaemons.org> * mdoc2man.rb: Properly put nested braces, parentheses and angles. diff --git a/lib/tempfile.rb b/lib/tempfile.rb index e68452fe6..b12279529 100644 --- a/lib/tempfile.rb +++ b/lib/tempfile.rb @@ -148,9 +148,25 @@ class Tempfile < SimpleDelegator } end - # Equivalent to new(). + # If no block is given, this is a synonym for new(). + # + # If a block is given, it will be passed tempfile as an argument, + # and the tempfile will automatically be closed when the block + # terminates. In this case, open() returns nil. def open(*args) - new(*args) + tempfile = new(*args) + + if block_given? + begin + yield(tempfile) + ensure + tempfile.close + end + + nil + else + tempfile + end end end end |
