Project

General

Profile

Actions

Feature #504

closed

Tempfile.open should return the new tempfile rather than nil

Added by TylerRick (Tyler Rick) about 16 years ago. Updated over 13 years ago.

Status:
Closed
Target version:
-
[ruby-core:18394]

Description

=begin
It seems like you ought to be able to do this
f = Tempfile.open("foo") do |f|
f.print("foo\n")
end
p f.path

but curretnly Tempfile.open returns nil.

I propose the following patch:

--- /usr/lib/ruby/1.9.0/tempfile.old.rb 2008-08-27 01:50:02.000000000 -0700
+++ /usr/lib/ruby/1.9.0/tempfile.rb 2008-08-27 01:51:03.000000000 -0700
@@ -182,15 +182,15 @@
tempfile = new(*args)

    if block_given?
  • begin
  • yield(tempfile)
  • ensure
  • tempfile.close
  • end
  •    begin
    
  •      yield(tempfile)
    
  •    ensure
    
  •      tempfile.close
    
  •    end
    
  • nil
  •    tempfile
     else
    
  • tempfile
  •    tempfile
     end
    

    end
    end
    @@ -204,5 +204,12 @@
    f.open
    p f.gets # => "foo\n"
    f.close!
    -end

  • f = Tempfile.open("foo") do |f|

  • f.print("foo\n")

  • end

  • p f.path # => "/tmp/foo20080827-5200-7awus9-0"

  • f.open

  • p f.gets # => "foo\n"

  • f.close!
    +end
    =end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0