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

**URL:** <https://www.ruby-forum.com/t/question-is-it-possible-for-an-extension-to-convert-a-block/72746>\
**Category:** Ruby\
**Created:** [February 24, 2006, 8:34am UTC](https://www.ruby-forum.com/t/question-is-it-possible-for-an-extension-to-convert-a-block/72746 "2006-02-24T08:34:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Repp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@Jacob\_Repp](https://www.ruby-forum.com/u/Jacob_Repp)\
**Post date:** [February 24, 2006, 8:34am UTC](https://www.ruby-forum.com/t/question-is-it-possible-for-an-extension-to-convert-a-block/72746/1 "2006-02-24T08:34:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jacob\_Repp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@Jacob\_Repp](https://www.ruby-forum.com/u/Jacob_Repp)\
**Post date:** [February 24, 2006, 10:08am UTC](https://www.ruby-forum.com/t/question-is-it-possible-for-an-extension-to-convert-a-block/72746/2 "2006-02-24T10:08:02Z")

</div>

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  
code  
}  
// 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 {  
@buffer  
def initialize()  
@buffer = “”  
end  
def recv\_callback(sock, buffer)  
@buffer \<\< buffer  
sock.recv(1024, self, :recv\_callback)  
end  
end

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  
}
