# Idiomatic way to pass multiple Procs/lambdas/blocks

**URL:** <https://www.ruby-forum.com/t/idiomatic-way-to-pass-multiple-procs-lambdas-blocks/188546>\
**Category:** Ruby\
**Created:** [May 13, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/idiomatic-way-to-pass-multiple-procs-lambdas-blocks/188546 "2010-05-13T18:07:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [May 13, 2010, 6:07pm UTC](https://www.ruby-forum.com/t/idiomatic-way-to-pass-multiple-procs-lambdas-blocks/188546/1 "2010-05-13T18:07:15Z")

</div>

I am working on a little syntactical sugar library for 0mq (networking  
library). One of the things I would like to provide is a set of  
asynchronous send/recv methods for transmitting and receiving messages.  
The method signature would look something like this:

def send message, timeout = -1, callback, errback  
end

where…

+timeout+ is the number of seconds to wait before triggering the  
+errback+ Proc; -1 disables timeouts

+callback+ is the Proc/method/lambda executed upon acknowledged  
completion

+errback+ is the Proc/method/lambda executed upon timeout

Ruby doesn’t allow for passing multiple blocks to a method so I’m faced  
with passing Procs or methods.

Is there a more idiomatic way to accomplish my intentions?

cr

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [May 13, 2010, 6:21pm UTC](https://www.ruby-forum.com/t/idiomatic-way-to-pass-multiple-procs-lambdas-blocks/188546/2 "2010-05-13T18:21:54Z")

</div>

I like using symbols that identify named methods, personally

---

<div class="post-metadata">

**Author:** ![cremes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cremes](https://www.ruby-forum.com/u/cremes)\
**Post date:** [May 13, 2010, 9:35pm UTC](https://www.ruby-forum.com/t/idiomatic-way-to-pass-multiple-procs-lambdas-blocks/188546/3 "2010-05-13T21:35:41Z")

</div>

On Thursday, May 13, 2010 11:03:37 am Chuck R. wrote:

> Ruby doesn’t allow for passing multiple blocks to a method so I’m faced  
> with passing Procs or methods.
> 
> Is there a more idiomatic way to accomplish my intentions?

Not really. In JavaScript, it’d look like this:

send({  
callback: function() { … },  
errback: function() { … },  
…  
});

I’d suggest that route, actually, if you’re going to have it just be a  
single  
method:

# 1.9-only syntax

send message, timeout: 123, callback: -\>{…}, errback: -\>{…}

# 1.8 syntax

send message, :timeout =\> 123, :callback =\> lambda{…}, :errback =\>  
lambda{…}

I’d also suggest a convenience class built around it, so I can instead  
do:

class Foo  
include YourModule

def callback  
…  
end  
def errback  
…  
end  
end

# Construct a new Foo and send a message with it

Foo.send(some\_message)
