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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E96756693ee0fc927a805ba0d509df72?d=identicon&s=25 Jacob Repp (Guest)
on 2006-02-24 08:34
(Received via mailing list)
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?
E96756693ee0fc927a805ba0d509df72?d=identicon&s=25 Jacob Repp (Guest)
on 2006-02-24 10:08
(Received via mailing list)
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
}
This topic is locked and can not be replied to.