Correct use of block

M

Martin

Hi

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?

Thanks.
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

Hi

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?

Thanks.

Personally, I think this is quite reasonable. I might rename the method
something like "call_and_time", and I'd probably leave off the empty parens
after the method name and the yield.
 
R

Robert Klemme

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
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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?


You can do

rsp = nil
t=time_method do
rsp = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
end

Since rsp was bound outside the scope of a block, it gets closed over, so
rebinding it will alter the outer variable (unlike Python's lambdas, or most
other languages for that matter)

You might consider using Ruby's built-in "benchmark" library as well:

require "benchmark"

rsp = nil
puts Benchmark.measure do
rsp = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top