summaryrefslogtreecommitdiff
path: root/lib/tempfile.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-20 12:27:53 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-20 12:27:53 +0000
commit244bb8db51a8417e299bed5cea19340a9a30ac33 (patch)
tree74920bcfcfb55c91c4be4d48b00e702a5c65aaf3 /lib/tempfile.rb
parent49123e550aa6fa21e6edfa09b58a73178dab5bd6 (diff)
* 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@3376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/tempfile.rb')
-rw-r--r--lib/tempfile.rb20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb
index e68452f..b122795 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