On 30.06.2010 17:31, Brian Candler wrote:
> Robert Klemme wrote:
>> 2010/6/30 Brian Candler<b.candler / pobox.com>:
>>> or rather:
>>>
>>> Tempfile.open "prefix", "/tmp" do |io|
>>> �o.write everything
>>> �o.flush
>>
>> I'd rather io.close instead of io.flush to release resources as soon
>> as possible.
>
> But tempfile will want to close itself using the block form anyway.

Yes, but later.  This can make a difference if you are low on file 
descriptors.  And you do not risk weird effects by the same process 
opening the file twice.

> In most versions of ruby, Tempfile with a block returns nil. A change
> was committed so that it returns the (closed) object, but that hasn't
> made it into either of the versions I have lying around here.
>
>>> tf = Tempfile.open("aaa","/tmp") { puts "hello"; 123 }
> hello
> =>  nil

The non block form obviously returns the Tempfile instance and if you 
want it to be returned from the block what stops you from explicitly 
returning it?

IMHO the method with block should return whatever the implementor of the lock chooses.  That is far more reusable than always returning the 
Tempfile.  Most of the time the Tempfile instance is of no use anyway 
since it is closed then.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/