Bug #504: Tempfile.open should return the new tempfile rather than nil http://redmine.ruby-lang.org/issues/show/504 Author: Tyler Rick Status: Open, Priority: Normal 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 ---------------------------------------- http://redmine.ruby-lang.org