On 11/29/2010 07:45 PM, Martin wrote:
> I've been using Python for years, but looking into Ruby and rails.
>
> Is the following a sensible way to use Ruby blocks?
>
> def time_method()
>      t1=Time.now
>      if block_given?
>          result=yield()
>      end
>      return Time.now-t1,result
> end
>
> t,rsp=time_method do
>      Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
> end
>
> My problem is that both time_method and get_response returns a value
> that i need. Should i use a closure instead of the above?

No, that's perfectly OK.  I would just do minor changes and leave out 
the test for block.  Your method does not make sense without a block so 
I'd just let it throw:

def time_method
   t = Time.now
   r = yield
   Time.now - t, r
end

I'd probably change the order of return values since the result of the 
block could be viewed as the more important result.

Kind regards

	robert