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
I like using symbols that identify named methods, personally
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)