diff options
Diffstat (limited to 'lib/tempfile.rb')
-rw-r--r-- | lib/tempfile.rb | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/tempfile.rb b/lib/tempfile.rb index e68452fe6a..b122795294 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 |