Using blocks recursively

12 views
Skip to first unread message

OrganicVeggie

unread,
May 24, 2012, 3:50:36 PM5/24/12
to mad-r...@googlegroups.com
So I'm trying to be overly clever (it happens) and write a recursive function in Ruby that uses blocks. I suspect I'm doing something wrong, because I'm not convinced it's working correctly. I know I could rewrite it to not be recursive, but now I'm just curious.

The example below is slightly simplified from the real code, but nothing important has been removed. Basically, it's supposed to take a block and attempt to execute it. If the block fails, it sleeps and re-executes the block using an exponential back-off. I believe the recursive call (inside the rescue block) is not passing in the block correctly.

Can anyone explain what I'm doing wrong?

---snip---
def run_with_backoff(log, max_time, sleep_time, message, &block)
  if sleep_time > max_time
    log.error "failed #{message}"
    return false
  end
  
  begin
    yield
  rescue
    log.warn "retrying #{message} in #{sleep_time} seconds"
    sleep sleep_time
    run_with_backoff(max_time, sleep_time * 2, message, block)
  end  
  true
end
---snip---

Brian Samson

unread,
May 24, 2012, 4:24:24 PM5/24/12
to mad-r...@googlegroups.com
You need to prefix an ampersand in front of block the second time.  You're also missing the "log" argument in the second call.  If you put the log back in to the method call, you'll see an error "expected 4 arguments instead of 5".  In that case, you're passing in the block not as a block argument but as a regular argument (a Proc object). That will change if you add an ampersand, i.e.: 

    run_with_backoff(log, max_time, sleep_time * 2, message, &block)

Cheers, 
Brian

--
You received this message because you are subscribed to the Google Groups "Mad Railers, the Madison, WI Ruby on Rails Users Group" group.
To visit the Madison Rails Home page, go to http://madisonrails.com/
To post to this group, send email to Mad-R...@googlegroups.com
To unsubscribe from this group, send email to Mad-Railers...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Mad-Railers?hl=en

OrganicVeggie

unread,
May 24, 2012, 5:00:52 PM5/24/12
to mad-r...@googlegroups.com, br...@briansamson.com
Ah hah! Nice catch on the missing log parameter. And your explanation of the passing the block as a Proc argument, instead of a block, was exactly what I needed.

Thanks!

-S


On Thursday, May 24, 2012 3:24:24 PM UTC-5, Brian Samson wrote:
You need to prefix an ampersand in front of block the second time.  You're also missing the "log" argument in the second call.  If you put the log back in to the method call, you'll see an error "expected 4 arguments instead of 5".  In that case, you're passing in the block not as a block argument but as a regular argument (a Proc object). That will change if you add an ampersand, i.e.: 

    run_with_backoff(log, max_time, sleep_time * 2, message, &block)

Cheers, 
Brian

Reply all
Reply to author
Forward
0 new messages