Question: is it possible for an extension to convert a block


I want to do this:

puts “sending data”
sock.send(“HELLO”) { |nbytes|
puts “sent #{nbytes} bytes”
puts “processing io completions”
queue.process() # calls the above defined block

I want to see the following outpout:

sending data
processing io completions
sent 5 bytes

Is this possible?


I found a good example of how to do this in hash.c

// how i implemented in my extension code (blocks get priority)
VALUE recvr, symb, proc;
int symbid;
rb_scan_args(argc, argv, “02&”, &recvr, &symb, &proc)
if(Qnil != proc)
recvr = proc;
symbid = rb_intern(“call”); // i actually cache this id in my init
// stores the recvr and symbid on a context object and executes the
async send
// in the background

ruby code, both ways work

sock.send(“HELLO”, myclient, :send_complete)
sock.send(“HELLO”) {
puts “send complete”

One reason why I provide the explicit recvr, symbol version is because
I haven’t figured out how to do the following using block notation:

class MyClient {
def initialize()
@buffer = “”
def recv_callback(sock, buffer)
@buffer << buffer
sock.recv(1024, self, :recv_callback)

Basically I need the capability to continue issuing the recv/send
calls and need to pass in the receiver of the completion when I issue
the call.

Is there something like?
s.recv(1024) {
s.recv(1024) this_block